gpt4 book ai didi

mysql - 结合两个查询sql

转载 作者:行者123 更新时间:2023-11-29 07:34:16 27 4
gpt4 key购买 nike

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

查询 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

我期望输出如下

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

有什么建议吗?

最佳答案

将 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;

希望这对您有所帮助!

关于mysql - 结合两个查询sql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728892/

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