【问题标题】:Combining two queries sql结合两个查询sql
【发布时间】:2018-04-09 08:45:59
【问题描述】:

我不知道如何组合以下查询

查询 1

SELECT hotel.country, time.year, time.month, COUNT(booking.room_id) as booked 

FROM booking 
LEFT JOIN room on room.room_id = booking.room_id 

LEFT JOIN hotel on room.hotel_id = hotel.hotel_id 

LEFT JOIN time on booking.time_id = time.time_id 

GROUP BY hotel.country, time.year, time.month 

ORDER by hotel.country, time.year, time.month

查询 2

SELECT hotel.country, time.year, time.month, COUNT(checkout.room_id) as checkedout

FROM checkout

LEFT JOIN room on room.room_id = checkout.room_id

LEFT JOIN hotel on room.hotel_id = hotel.hotel_id

LEFT JOIN time on checkout.time_id = time.time_id

GROUP BY hotel.country, time.year, time.month

ORDER BY hotel.country, time.year, time.month

我希望输出如下

国家、年份、月份、预订次数、结账次数、总和(预订+结账)

有什么建议吗?

【问题讨论】:

  • 联合所有。聚合。
  • 为什么是外连接?在 room_idhotel_idtime_id 为空的情况下是否存在预订和结账?这似乎没有多大意义。
  • 找工会???
  • 由于booking 指的是某个time,我推测这是预订的时间,而不是预订的时间(因为那将是一个时间范围 代替)。正确的?因此,我们可能会找到有预订但没有结账的月份,以及有结账但没有预订的月份。是吗?

标签: mysql sql


【解决方案1】:

使用 mysql Union 和聚合。

联合文档:https://dev.mysql.com/doc/refman/5.7/en/union.html 汇总文档:https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html

下面的联合示例:

SELECT 
    firstName, 
    lastName
FROM
    employees 
UNION 
SELECT 
    contactFirstName, 
    contactLastName
FROM
    customers;

希望这会有所帮助!

【讨论】:

    【解决方案2】:
    SELECT T1.country, 
           T1.year, 
           T1.month,
           T1.booked,
           T2.checkedout, 
          (T1.booked + T2.checkedout) AS 'sum(booking+checkout)'
    FROM 
    (
    SELECT hotel.country, time.year, time.month, COUNT(booking.room_id) as booked 
    
    FROM booking 
    LEFT JOIN room on room.room_id = booking.room_id 
    
    LEFT JOIN hotel on room.hotel_id = hotel.hotel_id 
    
    LEFT JOIN time on booking.time_id = time.time_id 
    
    GROUP BY hotel.country, time.year, time.month 
    
    ORDER by hotel.country, time.year, time.month
    ) AS T1
    INNER JOIN
    (
    SELECT hotel.country, time.year, time.month, COUNT(checkout.room_id) as checkedout
    
    FROM checkout
    
    LEFT JOIN room on room.room_id = checkout.room_id
    
    LEFT JOIN hotel on room.hotel_id = hotel.hotel_id
    
    LEFT JOIN time on checkout.time_id = time.time_id
    
    GROUP BY hotel.country, time.year, time.month
    
    ORDER BY hotel.country, time.year, time.month
    )AS T2
    ON T1.country=T2.country
    AND T1.year=T2.year
    AND T1.month=T2.month
    

    【讨论】:

      【解决方案3】:

      解决此问题的一种方法是使用UNON ALL

      SELECT h.country, t.year, t.month, SUM(is_booking) as booked, SUM(is_checkout) as checkedout
      FROM ((SELECT b.room_id, b.time_id, 1 as is_booking 0 as is_checkout
             FROM booking b
            ) UNION ALL
            (SELECT c.room_id, c.time_id, 0 as is_booking, 1 as is_checkout
             FROM checkout c
            )
           ) bc  LEFT JOIN
           room r
           ON r.room_id = bc.room_id LEFT JOIN
           hotel h
           ON r.hotel_id = h.hotel_id LEFT JOIN
           time t
           ON bc.time_id = t.time_id 
      GROUP BY h.country, t.year, t.month 
      ORDER by h.country, t.year, t.month
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-14
        • 2021-07-02
        • 2016-10-27
        • 1970-01-01
        • 1970-01-01
        • 2012-11-14
        • 2016-09-28
        相关资源
        最近更新 更多