4个表的复杂sql查询

发布于 2024-12-29 14:46:12 字数 1306 浏览 1 评论 0原文

我正在开发一份包含很多酒店的在线旅行指南。每家酒店都属于一个特定的类别,有很多房型,并且每个酒店房间每个季节都有不同的价格。我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尛丟丟 2025-01-05 14:46:12
SELECT  Categories.Name, COUNT(DISTINCT ID_Hotel) [Count]
FROM    Hotels
        INNER JOIN Categories
            ON Category_ID = ID_Category
        INNER JOIN
        (   SELECT  Hotel_ID, MIN(Price) [LowestPrice]
            FROM    hotels_room_types
                    INNER JOIN hotels_room_types_seasons
                        ON id_hotels_room_type = hotels_room_types_id
            -- CONSIDER FILTERING BY SEASON HERE
            GROUP BY Hotel_ID
        ) price
            ON price.Hotel_ID = Hotels.ID_Hotel
WHERE   LowestPrice BETWEEN 10 AND 130 -- OR WHATEVER YOUR PARAMETERS ARE
GROUP BY Categories.Name

我不知道您正在使用什么 RDBMS,但我不知道您的查询将在哪里工作。您在最低价格方面遇到的问题(我假设)是因为您在按类别分组后应用逻辑,因此您正在计算该类别的最低价格在 10 到 130 之间的所有酒店,而不是酒店有房间的酒店最低价格在10到130之间。

SELECT  Categories.Name, COUNT(DISTINCT ID_Hotel) [Count]
FROM    Hotels
        INNER JOIN Categories
            ON Category_ID = ID_Category
        INNER JOIN
        (   SELECT  Hotel_ID, MIN(Price) [LowestPrice]
            FROM    hotels_room_types
                    INNER JOIN hotels_room_types_seasons
                        ON id_hotels_room_type = hotels_room_types_id
            -- CONSIDER FILTERING BY SEASON HERE
            GROUP BY Hotel_ID
        ) price
            ON price.Hotel_ID = Hotels.ID_Hotel
WHERE   LowestPrice BETWEEN 10 AND 130 -- OR WHATEVER YOUR PARAMETERS ARE
GROUP BY Categories.Name

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.

归途 2025-01-05 14:46:12
select
      c.Category_name,
      count(*) NumHotels
   from
      ( select distinct
              byRoomType.hotel_id
           from
              hotels_room_types_seasons bySeason
                 join hotels_room_types byRoomType
                    on bySeason.hotels_room_types_id = byRoomType.id_hotels_room_type
           where
              bySeason.Price between LowPriceParameter and HighPriceParameter 
      ) QualifiedHotels

         join Hotels
            on QualifiedHotels.hotel_id = Hotels.id_hotel

            join Categories c
               on category_id = c.id_category
select
      c.Category_name,
      count(*) NumHotels
   from
      ( select distinct
              byRoomType.hotel_id
           from
              hotels_room_types_seasons bySeason
                 join hotels_room_types byRoomType
                    on bySeason.hotels_room_types_id = byRoomType.id_hotels_room_type
           where
              bySeason.Price between LowPriceParameter and HighPriceParameter 
      ) QualifiedHotels

         join Hotels
            on QualifiedHotels.hotel_id = Hotels.id_hotel

            join Categories c
               on category_id = c.id_category
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文