Take out an sql query

2

Hello, good morning and I look forward to your help. I need to get out of the table

restaurant:

and the table user:

all those restaurants that do not belong to a user in this case is 3 (empty restaurant name), because as you can see in the user table the id_restaurante of each user is either 1 or 2.

I've been doing this for a while now, I've done this but he does not take it out:

select r.id, r.nombre, r.direccion, r.ciudad, r.provincia, r.cp, r.cif, r.imagen_url, r.horario 

from restaurante r inner join usuario u on u.id_restaurante = r.id

where u.id_restaurante != r.id
group by r.id 
    
asked by T P 20.05.2017 в 10:17
source

1 answer

1

In your query INNER JOIN you will not get those elements that do not have a relationship established between both tables.

You must use LEFT JOIN o RIGHT JOIN to list all records from one of the two tables (right or left) regardless of whether there is one related in the other table.

When using a relation of this type the non-existent elements of the other table will appear as values NULL , so we can search any field (I have chosen u.id , but it could have been another one that can not have value NULL , as u.id_restaurante ) of that table that is NULL , for example:

SELECT r.*
FROM restaurante r
LEFT JOIN
  usuario u
  ON u.id_restaurante = r.id
WHERE u.id IS NULL
    
answered by 20.05.2017 / 10:36
source