2015-08-28 18 views
0

有2个表T1和T2 sql是否有任何变体在T2关联行不存在的情况下从T1中选择所有行?没有JoinLeftSQL选择连接表中的行是否为空

例如

table user 
id---name 
1  A 
2  B 

task 
id----idUser 
1  1 

结果== 2,B BR!

+0

什么是不使用Leftjoin的原因是什么?你可以通过左加入轻松实现这一点 –

+0

我认为这个查询不会工作,当你有不同的用户名的第一个表中有多个记录的id = 2 –

回答

1

的典型方法是使用not in

select u.* 
from user u 
where u.id not in (select iduser from task); 

然而,not exists具有更好的语义,因为它处理NULL值更直观:

select u.* 
from user u 
where not exists (select 1 from task t where t.iduser = u.id);