Union of more than one table

Asked

Viewed 24 times

0

In my database there are two tables:

  • cde_usuario
  • cde_venda_detail

I will begin to introduce a part of Notifications system. The record must have some column indicating whether the notification has been viewed or not.

So I added the column notification in both tables. When the value is 0, it was not visualized, when 1, she was visualized.

Example of records of the two tables:

cde_usuario

| id | nome  | notificacao |
| 1  | Luan  | 1           |
| 2  | Carlos| 1           |
| 3  | Maria | 0           |

cde_venda_detail

| num_venda | valor | notificacao |
| 1         | 50.0  | 1           |
| 2         | 20.0  | 0           |
| 3         | 9.99  | 0           |

I made this query to return the regimens that NAY were viewed:

$sqlnotificacao = "SELECT cde_usuario.*, cde_venda_detalhe.* FROM cde_usuario, cde_venda_detalhe WHERE cde_usuario.notificacao = 0 AND cde_venda_detalhe.notificacao = 0";

However, it always returns only the table records cde_venda_detail, in the case of the sale of 2 and 3. I would like to know what I should change in this my query to also return the table records cde_usuario, in the case, the Maria.

  • 1

    As the tables do not seem to me to be related (at least with what you showed) it seems to me that a possible solution would be to use UNION of queries to each of the tables, however it seems to me that the result of the queries are not union-compatible. Maybe this can be circumvented if you add a value field containing 0 in the cde_usuario table and a name field containing a null string in the cde_venda_detail table.

  • I only put these fields to get the question to be less polluted, but actually in the cde_venda_detail table has the id_client field. I wanted a way to do this without them being related, in fact, in the future I will add other tables as for example one of tasks and products that I also have here.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.