首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

请问一个SQL语句

2011-12-24 
请教一个SQL语句.我的数据库是DB2.现在有两张表A,B.A中字段id phone,user,timeB中有字段 id phone time现

请教一个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

热点排行