0

下面是我正在尝试使用的查询,

select DISTINCT c.sno from Cards as c, reservation as r 
where c.name='CRS-4-FC' AND c.sno != r.ssno;

这个查询应该只从卡中选择那些不存在于保留表中的 sno,即保留表中的 ssno 列中不存在 sno。我投入

c.sno!=r.ssno

这样我就不会选择所有的sno。但是当这个查询运行时,我得到了所有的 sno 而不是那些没有保留的。有人可以帮我解决我哪里出错了..

4

3 回答 3

2

尝试

select DISTINCT c.sno
from Cards as c 
left outer join reservation as r on c.sno = r.ssno
where r.ssno is null
and c.name='CRS-4-FC'
于 2012-08-10T10:41:54.230 回答
1

LEFT JOIN改为尝试:

SELECT DISTINCT c.sno
FROM Cards c
    LEFT JOIN reservation as r
        ON c.sno = r.ssno
WHERE c.name='CRS-4-FC' AND
      r.ssno IS NULL;

请参阅SQL 连接的可视化解释

于 2012-08-10T10:42:41.707 回答
1
select DISTINCT c.sno 
from Cards as c LEFT JOIN reservation as r  
ON c.sno = r.ssno
where c.name='CRS-4-FC' AND r.ssno IS NULL; 
于 2012-08-10T10:43:27.960 回答