3

我有这个查询。第二部分是获取正确的值,但最终输出数据仍然包括列等于选择值的行。

SELECT * 
FROM roomfacilities 
WHERE room <> '(SELECT room_assigned 
                FROM allocation 
                WHERE booking_id = 01010106)';

我试图让它工作,说 SELECT all from roomfacilitiesWHERE room 'does not equal' 来自选择的结果。

谢谢你的帮助。

4

2 回答 2

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;
于 2013-02-18T13:50:13.670 回答
3

当 from table 的值为 时,您可以通过使用从table中LEFT JOIN获取所有记录。roomfacilitiesroom_assignedallocationNULL

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
于 2013-02-18T13:50:51.313 回答