gpt4 book ai didi

MySQL - 仅加入 1 个结果或受其他表限制

转载 作者:行者123 更新时间:2023-11-29 03:09:37 24 4
gpt4 key购买 nike

我目前在使用 MySQL 时遇到了一个恼人的问题。

我从通过以下查询从我的数据库中获取酒店开始:

SELECT `Hotel`.*,
`Hotellocations`.`address`,
`Hotellocations`.`city`,
`Hotellocations`.`lat`,
`Hotellocations`.`lon`,
`Hotelfacilities`.`facilities`,
`hotel_rooms` AS `hotelroomS`
FROM `hotels` AS `Hotel`
LEFT JOIN `hotel_locations` AS `Hotellocations`
ON (`Hotel`.`hotelid` = `Hotellocations`.`hotelid`)
LEFT JOIN `hotel_facilities` AS `Hotelfacilities`
ON (`Hotel`.`hotelid` = `Hotelfacilities`.`hotelid`)
WHERE MATCH(`hotel`.`title`) AGAINST("+Amsterdam" IN BOOLEAN MODE)
AND `hotel`.`min_price` != 0 AND `Hotel`.`min_price` >= 0
AND `Hotel`.`max_price` <= 500
LIMIT 1

我现在正在尝试加入一个新表,即“hotel_rooms”表,其中有一列名为“num_people”,它表示可以留在房间里的人数。

我现在的问题是,如何加入这张 table ?我尝试了 inner joining this,但随后 MySQL 返回多行(来自 hotel_rooms),因为一家酒店可能有多个房间..

我的酒店表是这样的:

id | hotelid | title | slug | description | min_price | max_price | stars | checkin | checkout

hotel_rooms 表是这样的:

roomid | hotelid | ratetypeid | providerid | title | description | num_people | min_price | calendarcode

我真的希望你能帮助我。:) 如果你需要更多信息,请告诉我。

亲切的问候,

罗宾

最佳答案

您似乎需要酒店的可用房间总数。不要直接加入 hotel_rooms 表,而是将加入作为,

join ( select hotelid, sum(num_people) as hotel_rooms
from hotel_rooms
group by hotelid) hr
on hr.hotelid = hotel.hotelid

关于MySQL - 仅加入 1 个结果或受其他表限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10294068/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com