简体   繁体   English

这两个查询有什么区别?

[英]Whats the difference between these two queries?

SELECT DISTINCT S.sname
FROM   student S,
       enrolled E1,
       enrolled E2,
       class C1,
       class C2
WHERE  S.snum = E1.snum
       AND S.snum = E2.snum
       AND E1.cname <> E2.cname
       AND E1.cname = C1.NAME
       AND E2.cname = C2.NAME
       AND C1.meets_at = C2.meets_at;  

and

SELECT DISTINCT S.sname
FROM   student S
WHERE  S.snum IN (SELECT E1.snum
                  FROM   enrolled E1,
                         enrolled E2,
                         class C1,
                         class C2
                  WHERE  E1.snum = E2.snum
                         AND E1.cname <> E2.cname
                         AND C1.meets_at = C2.meets_at);  

both the queries are executing and showing output The first query is showing output as empty set while the second query is showing some non empty output两个查询都在执行并显示 output 第一个查询显示 output 为空集,而第二个查询显示一些非空 output

I thought both the queries will give me the same and correct output but only second query is showing correct result.我认为这两个查询都会给我相同且正确的 output 但只有第二个查询显示正确的结果。 Can anyone please explain whats the difference between both the queries?谁能解释一下这两个查询之间的区别是什么? Thanks in advance.提前致谢。

There is no join relationship specified between the "Es" and the "Cs" in the second query so that's going to be a cross join (everything matches with everything).在第二个查询中,“Es”和“Cs”之间没有指定连接关系,因此这将是一个交叉连接(一切都与一切匹配)。 You have this in the first query and nothing comparable in the second:您在第一个查询中有这个,而在第二个查询中没有可比性:

       AND E1.cname = C1.NAME
       AND E2.cname = C2.NAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM