MySQL-选择收集第二个选择结果的位置

时间:2013-02-18 13:49:05

标签: mysql sql database select

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

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

我正试图让它工作说明所有来自roomfacilities所有房间'不等于'结果来自选择。

感谢您的帮助。

2 个答案:

答案 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,当表格roomfacilitiesroom_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