我通过以下查询联接两个表:
select * from t1
LEFT JOIN t2 ON t1.First1=t2.FirstName
and t1.Last1=t2.LastName
and t1.City=t2.City
and t1.ST=t2.State;
现在,如何运行单独的查询以打印出t2中未加入的行?
我尝试对字段使用NOT IN,但是收到错误消息:错误代码:1241。操作数应包含1列
select * from t2 where t2.RefID NOT IN (
select * from t1,t2 where t1.First1=t2.FirstName
and t1.Last1=t2.LastName
and t1.City=t2.City
and t1.ST=t2.State);
答案 0 :(得分:1)
尝试一下:
SELECT * FROM t2
WHERE NOT EXISTS(SELECT 1 FROM t1
WHERE t1.First1=t2.FirstName
AND t1.Last1=t2.LastName
AND t1.City=t2.City
AND t1.ST=t2.State);
在使用NOT IN
之前,请记住:
关于NOT EXISTS和NOT IN的最重要说明是,与EXISTS和IN不同,它们并非在所有情况下都是等效的。具体来说,当涉及NULL时,它们将返回不同的结果。具体来说,当子查询返回一个null时,NOT IN将不匹配任何行。
答案 1 :(得分:1)
翻转连接并检查不匹配的内容...
select * from t2
LEFT JOIN t1 ON t1.First1=t2.FirstName
and t1.Last1=t2.LastName
and t1.City=t2.City
and t1.ST=t2.State
WHERE t1.someid IS NULL
;