Select union between tables

1

I have two tables, CLIENTS and SALES. I would like to know which clients have no sales, and I store the customer ID in the sales table.

I tried this but to no avail:

SELECT *
FROM cliente
RIGHT JOIN vendas ON (vendas.id_cliente = cliente.id)
WHERE vendas.id_cliente IS NULL
    
asked by anonymous 23.04.2015 / 20:42

3 answers

3

Use the NOT EXISTS clause

SELECT * FROM cliente
WHERE NOT EXISTS(SELECT vendas.id_cliente FROM vendas WHERE vendas.id_cliente = cliente.id)
If the subquery does not return records it is because this client id does not exist in the sales table, this makes EXISTS false,

    
23.04.2015 / 20:46
0

Hello, Try this :

SELECT *
FROM cliente
RIGHT JOIN vendas ON (vendas.id_cliente = cliente.id)
WHERE vendas.id_cliente = NULL
    
23.04.2015 / 21:52
0

Try with LEFT JOIN , like this:

SELECT *
FROM cliente
LEFT JOIN vendas ON (vendas.id_cliente = cliente.id)
WHERE vendas.id_cliente IS NULL
    
23.04.2015 / 21:33