日期:2014-05-18  浏览次数:20803 次

请教一个SQL语句.
我的数据库是DB2.

现在有两张表A,B.
A中字段 id phone,user,time
B中有字段 id phone time

现在我拿两张表做比较,要得到 A中 phone字段比 B 中phone字段多的数据.罗列出来.实际也就是求差集, 其中除ID外所有字段数据都是允许重复的.
SQL该如何写啊.

我写的如下:select * from A where not exists (select * from B where A.phone = B.phone)
但得到结果不对.


------解决方案--------------------
你说的问题不清楚
你要的结果是不是说要查询A表中存在而B表中不存在的电话号码啊。
select * from A where not exists (select 1 from B where A.phone = B.phone ) 
这个SQL看来本没有什么问题,如果你的表中的电话号码为空的话,结果可能就不对了
任何数和空比都得空,check 下。
这样的话最好在加上 a.phone is not null and b.phone is not null