1

SQL クエリに問題があります

これが私のクエリです:

SELECT `rooms`.*  
FROM `rooms`  
WHERE (`rooms`.`room_id` NOT IN (
    SELECT * 
    FROM `reservations` `res` 
    WHERE ('2012-05-03' NOT BETWEEN res.expected_checkin_date 
    AND res.expected_checkout_date) 
    AND ('2012-05-08' NOT BETWEEN res.expected_checkin_date 
    AND res.expected_checkout_date)))

問題は、2 番目selectが null を返すと、次のエラーが表示されることです。

#1241 - Operand should contain 1 column(s)

誰でも私を助けることができますか?

EDIT

このクエリを実行すると、select 理由がわかりませんが、2 番目の行を含む行も含めてすべての行が表示されます。

SELECT `rooms`.*, `type`.*, `status`.* FROM `rooms` INNER JOIN `room_types` AS `type` ON `type`.`room_type_id` = `rooms`.`room_type_id` AND `type`.`num_beds` >= 1 AND type.room_type_id=2 INNER JOIN `room_statuses` AS `status` ON `status`.`room_status_id` = `rooms`.`room_status_id` WHERE (`rooms`.`room_id` NOT IN (SELECT `res`.`room_id` FROM `reservations` `res` WHERE (res.expected_checkin_date >= '2012-05-01' AND res.expected_checkin_date < '2012-05-31') AND (res.expected_checkout_date >= '2012-05-31' AND res.expected_checkout_date < '2012-05-31')))
4

2 に答える 2

6

あなたは次のようなものが欲しいと思います

SELECT `reservations`.`room_id` FROM `reservations`

副選択の場合は、演算子room_idと照合しているものです。NOT IN

于 2012-05-19T16:00:12.270 に答える
4
SELECT `rooms`.*  
FROM `rooms`  
WHERE (`rooms`.`room_id` NOT IN (
    SELECT * 
    FROM `reservations` `res` 
    WHERE ('2012-05-03' NOT BETWEEN res.expected_checkin_date 
    AND res.expected_checkout_date) 
    AND ('2012-05-08' NOT BETWEEN res.expected_checkin_date 
    AND res.expected_checkout_date)))

NOT inは、where内の同じ値のリストに制限する必要があります。*予約にroom_Idしかない場合を除き、*は機能しません。

SELECT `rooms`.*  
FROM `rooms`  
WHERE (`rooms`.`room_id` NOT IN (
    SELECT res.Room_ID 
    FROM `reservations` `res` 
    WHERE ('2012-05-03' NOT BETWEEN res.expected_checkin_date 
    AND res.expected_checkout_date) 
    AND ('2012-05-08' NOT BETWEEN res.expected_checkin_date 
    AND res.expected_checkout_date)))
于 2012-05-19T16:03:36.607 に答える