2013-02-18 115 views
3

我有這個查詢。第二部分是獲取正確的值,但最終的輸出數據仍然包含列等於select的值的行。MySQL-選擇收集第二個選擇結果的地方

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

我試圖得到它的工作說SELECT全部來自roomfacilities WHERE房「不等於」從選擇的結果。

感謝您的任何幫助。

回答

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; 
3

使用LEFT JOIN你可以從表roomfacilities所有記錄時的room_assigned從表allocationNULL

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