大家好我正在尝试合并两个查询:
SELECT DISTINCT name,$price
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>=$partySize
和
SELECT DISTINCT room_id
FROM room_booking
WHERE room_id NOT IN
(SELECT DISTINCT room_id
FROM room_booking
WHERE date_booked = '$date'
)
合并上述内容的最佳方法是什么,以便最终查询检查room_booking表中不存在的日期,并在房间表超出给定容量时从房间表中提取相关信息。
答案 0 :(得分:1)
这应该完成文本中描述的工作(但我不确定date_booked<>'$us_date'
应该做什么):
select name, price
from room
where capacity >= $partySize
and room_id not in
(select room_id
from room_booking
where date_booked = '$date'
)
答案 1 :(得分:1)
尝试以下列方式合并:
SELECT DISTINCT name,$price
FROM room JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>=$partySize AND room_booking.room_id IN (SELECT DISTINCT room_id
FROM room_booking
WHERE room_id NOT IN
(SELECT DISTINCT room_id
FROM room_booking
WHERE date_booked = '$date'
));
答案 2 :(得分:1)
也许
SELECT DISTINCT name,$price
FROM room
JOIN room_booking ON room.room_id=room_booking.room_id
WHERE date_booked<>'$us_date' AND capacity>=$partySize
AND date_booked <> '$date'
或者您是否也想列出所有room_id?然后将room_id添加到选择列表中。在这种情况下你可以删除distinct,因为我认为room_id是唯一的