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

SQL请教
查询两个表中不同的,not EXISTS 
以下两个语句有什么不同?第二个语句是正确的。bsperson.jz这里有什么问题?

SQL code
select distinct jz,lb from bsperson where not EXISTS 
(select chuqin.jz,chuqin.lb [color=#FF0000]from chuqin,bsperson where chuqin.jz=bsperson.jz and chuqin.lb=bsperson.lb[/color])


SQL code
select distinct jz,lb from bsperson where not exists 
(select jz,lb [color=#FF0000]from chuqin where chuqin.jz=bsperson.jz and chuqin.lb=bsperson.lb[/color])


------解决方案--------------------
第1个里面有两个 相同的表 bsperson ,都没有指定别名,所以无法确定 bsperson.js 是哪个表的
------解决方案--------------------
select distinct jz,lb from bsperson where not EXISTS 
(select chuqin.jz,chuqin.lb from chuqin,bsperson where chuqin.jz=bsperson.jz and chuqin.lb=bsperson.lb)

你这里有两个bsperson,就等于是有两个张三,我叫一声张三,你也不知道我叫谁
很显然,子查询里的bsperson是多余的
------解决方案--------------------
探讨
意思就是说,引用子查询时,必须用前一个语句中的表?这样对吗?