日期:2014-05-16 浏览次数:20492 次
网站来源: http://topic.csdn.net/u/20090828/10/293002b9-b37e-49fa-ba42-a4aba34a6b9d.html
?
exists (sql 返回结果集为真)
not exists (sql 不返回结果集为真)
如下:
表A
ID NAME
1??? A1
2??? A2
3? A3
表B
ID AID NAME
1??? 1 B1
2??? 2 B2
3??? 2 B3
表A和表B是1对多的关系 A.ID => B.AID
SELECT ID,NAME FROM A WHERE EXIST (SELECT * FROM B WHERE A.ID=B.AID)
执行结果为
1 A1
2 A2
原因可以按照如下分析
SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B WHERE B.AID=1)
--->SELECT * FROM B WHERE B.AID=1有值返回真所以有数据
SELECT ID,NAME FROM A WHERE EXISTS (SELECT * FROM B <