在表sc中有三列属性,sno,cno,grade,为什么SQL语句
select cno
from sc scx
where cno not in
(select cno from sc where sno<>scx.sno);和
select cno
from sc scx
where cno not in
(select cno from sc where sno<>sno);查询结果不同?
select cno
from sc scx
where cno not in
(select cno from sc where sno<>scx.sno);和
select cno
from sc scx
where cno not in
(select cno from sc where sno<>sno);查询结果不同?
第2个查询没有结果?
from sc scx
where cno not in
(select cno from sc where sno<>sno);第二个,这里 sno<>sno 永远是相等的,数据库会用当前表的两个字段比较,所以恒等,也就是说所有的记录都符合条件被返回。而 sno<>scx.sno 则指明是和外面的表scx进行比较。
from sc scx
where cno not in
(select cno from sc where sc.sno<>scx.sno);
最好写子查询的时候,表名都带上,这样肯定不会有错,而且看起来也很容易理解。