I try to get this query different

2

I need to draw from these three tables:

user:

user_type

restaurant:

all those restaurants that do not belong or do not have a type_user 1 (owner), that is to say in this case I would need to take out restaurant winery and restaaa that they have no owner user.

What I have tried is this but nothing ....

select r.* 
  from restaurante r
 inner join usuario u 
    on u.id_restaurante = r.id 
  left join tipo_usuario t
    on t.id = u.id_tipo_usuario
 where u.id_tipo_usuario = 1 
   and u.id_restaurante is null
    
asked by T P 21.05.2017 в 18:26
source

1 answer

2

You only need to scroll through your restaurant table and with the sub select exclude users that meet the condition you need to remove.

select * from restaurante 
   where id not in ( select id_restaurante from usuario 
                      where id_tipo_usuario = 1 or id_restaurante is null );

Previously my answer was this, but I did not like it at all:

select r.* from restaurante r inner join usuario u 
         on u.id_restaurante = r.id 
                              left join tipo_usuario t 
         on t.id = u.id_tipo_usuario 
       where u.id_restaurante not in ( select id_restaurante from usuario 
                                           where id_tipo_usuario = 1 
                                             or  id_restaurante is null);
    
answered by 21.05.2017 в 19:12