4个表的复杂sql查询
我正在开发一份包含很多酒店的在线旅行指南。每家酒店都属于一个特定的类别,有很多房型,并且每个酒店房间每个季节都有不同的价格。我想从 4 个表中进行复杂查询,以获得每个酒店类别的酒店总数其中每个酒店客房的最低价格介于 2 个由滑块调整的值之间。
我的表格如下所示:
- 类别
- id_category
- Category_name
- 酒店
- id_hotel
- hotel_name
- Category_id
- ......
- hotels_room_types
- id_hotels_room_type
- hotel_id
- room_type_id
- ......
- < b>hotels_room_types_seasons
- hotels_room_types_id
- season_id
- 价格
- ......
例如category_name的一些值是:酒店、公寓、旅馆
我希望我的结果表有两个字段,如下所示:
Hotels 32
公寓 0
Hostels 5
我尝试了以下查询,但它返回每个类别的所有酒店总数,而不是客房最低价格介于价格范围之间的酒店数量。
SELECT c.category_name, count( DISTINCT id_hotel ) , min( price ) min_price
FROM categories c
LEFT JOIN hotels w ON ( c.id_category = w.category_id )
LEFT JOIN (
hotels_room_types
INNER JOIN hotels_room_types_seasons ON hotels_room_types.id_hotels_room_types = hotels_room_types_seasons.hotels_room_types_id)
ON w.id_hotel = hotels_room_types.hotel_id
GROUP BY c.category_name
HAVING min_price >=10 AND min_price <=130
谁能帮助我如何编写适当的查询?
谢谢!!!
I am developing an online travel guide with a lot of hotels. Each hotel belongs to a specific category, has a lot room types and each of hotel room has different price per season. I want to make a complex query from 4 tables in order to get the total number of hotels per hotels category where the minimum price of each hotel rooms is between 2 values which are adjusted by a slider.
My tables look like:
- Categories
- id_category
- category_name
- Hotels
- id_hotel
- hotel_name
- category_id
- ......
- hotels_room_types
- id_hotels_room_type
- hotel_id
- room_type_id
- ......
- hotels_room_types_seasons
- hotels_room_types_id
- season_id
- price
- ......
for example some values of category_name are: Hotels, apartments, hostels
I would like my results table to have two fields like the following:
Hotels 32
apartments 0
hostels 5
I tried the following query but it returns the total number of all hotels per category, not the number of hotels where the minimum price of their rooms is between the price range.
SELECT c.category_name, count( DISTINCT id_hotel ) , min( price ) min_price
FROM categories c
LEFT JOIN hotels w ON ( c.id_category = w.category_id )
LEFT JOIN (
hotels_room_types
INNER JOIN hotels_room_types_seasons ON hotels_room_types.id_hotels_room_types = hotels_room_types_seasons.hotels_room_types_id)
ON w.id_hotel = hotels_room_types.hotel_id
GROUP BY c.category_name
HAVING min_price >=10 AND min_price <=130
Could anyone help me how to write the appropriate query?
Thanks!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道您正在使用什么 RDBMS,但我不知道您的查询将在哪里工作。您在最低价格方面遇到的问题(我假设)是因为您在按类别分组后应用逻辑,因此您正在计算该类别的最低价格在 10 到 130 之间的所有酒店,而不是酒店有房间的酒店最低价格在10到130之间。
I have no idea what RDBMS you are using but I do not know any where your query would work. The problem you were having with the Min Price (I assume) is because you are applying the logic after grouping by category, so you are counting all hotels where the category has a lowest price between 10 and 130, not where the hotel has a room with the lowest price between 10 and 130.