它是一个需要解决的查询

时间:2013-06-05 06:53:20

标签: mysql

有两个表

Table 1
GroupName     Resolver
Aa                 A
Bb                 B
Cc                 C
Dd                 D

Table 2
key       UserId
1           B
2           C

输出应该是A和D.这意味着表2中不存在的记录应该来了。

4 个答案:

答案 0 :(得分:1)

select GroupName,Resolver
from Table1
where Resolver not in (select UserId from Table2)

<强> SQL Fiddle 1

另一个查询

select t1.GroupName,t1.Resolver
from Table1 t1
left join Table2 t2 
on t1.Resolver=t2.UserId
where t2.UserId is null

<强> SQL Fiddle 2

答案 1 :(得分:1)

select * from table1 t1 
left join table2 t2 on t1.Resolver = t2.UserId 
where t2.UserId is null

使用左连接并检查null

<强> SQLFiddle

答案 2 :(得分:0)

使用以下查询:

Select * from Table1 where Resolver NOT IN (SELECT UserId from Table2);

答案 3 :(得分:0)

select t1.Resolver from Table1 t1, table t2 where t1.Resolver != t2.UserId