我有这个查询。第二部分是获取正确的值,但最终输出数据仍然包括列等于select值的行。
SELECT *
FROM roomfacilities
WHERE room <> '(SELECT room_assigned
FROM allocation
WHERE booking_id = 01010106)';
我正试图让它工作说明所有来自roomfacilities
所有房间'不等于'结果来自选择。
感谢您的帮助。
答案 0 :(得分:4)
使用NOT IN
谓词:
SELECT *
FROM roomfacilities
WHERE room NOT IN(SELECT room_assigned
FROM allocation
WHERE booking_id = 01010106);
或: LEFT JOIN
:
SELECT f.*
FROM roomfacilities f
LEFT JOIN allocation a ON f.room = a.room_assigned
AND a.booking_id = 01010106
WHERE a.room_assigned IS NULL;
答案 1 :(得分:3)
使用LEFT JOIN
,当表格roomfacilities
中room_assigned
的值为allocation
时,您可以从表NULL
获取所有记录。
SELECT a.*
FROM roomfacilities a
LEFT JOIN allocation b
ON a.room = b.room_assigned AND
b.booking_id = 01010106
WHERE b.room_assigned IS NULL