0

我有一个小任务:找到那些有投影仪但没有白板的房间。

CREATE TABLE Equipment 
(
room VARCHAR(15),
type VARCHAR(20)
);

INSERT INTO Equipment VALUES ('Dreyer-201','projector');
INSERT INTO Equipment VALUES **('Zuse-127','projector');**
INSERT INTO Equipment VALUES ('Shannon-164','projector');
INSERT INTO Equipment VALUES ('Dreyer-201','whiteboard');
INSERT INTO Equipment VALUES **('Zuse-127','whiteboard');**
INSERT INTO Equipment VALUES ('Shannon-164','whiteboard');

这是我的回答:

SELECT DISTINCT room, type
FROM Equipment
WHERE type = 'projector' AND NOT type = 'whiteboard'

它正在工作 - 但我的输出列出了房间 Zuse-127。我知道它独立于投影仪和白板,但我该如何解决这个问题?所以房间 Zuse-127 没有显示。

4

2 回答 2

0

试试这个NOT IN()用于属于白板的房间

SELECT DISTINCT room, type
FROM Equipment
WHERE type = 'projector' AND room
 NOT IN (SELECT `room` FROM Equipment WHERE type = 'whiteboard' )
于 2013-09-18T18:25:35.777 回答
0

自联接可能比使用 GROUP BY 或子查询的解决方案执行得更好。

SELECT e.room, e.type
FROM Equipment AS e
LEFT OUTER JOIN Equipment AS e2
 ON e.room = e2.room AND e2.type = 'whiteboard'
WHERE e.type = 'projector'
 AND e2.type IS NULL;

(type,room)为获得最佳结果添加索引。

于 2013-09-18T18:29:46.157 回答