gpt4 book ai didi

mysql - 合并并显示查询结果

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

我有 2 个查询:

SELECT CustomerID,count(b.BookingStatus) as 'NotComplete'
FROM Booking b, Customer c
WHERE c.CustomerID=b.BookingCustomerID
AND(b.BookingStatus='Pending'
OR b.BookingStatus='OTW')
GROUP BY c.CustomerID


SELECT c.CustomerID, r.*
FROM Customer c,Regular r
WHERE c.CustomerID=r.RegularCID
Result:

第一个查询 enter image description here

第二个查询 enter image description here

如何将这两个结果结合在一起?

此外,还显示零(计数)。

谢谢!

<小时/>

这是我经过几个小时的尝试后得到的结果..显然这不是我想要的..

SELECT c.CustomerID,count(b.BookingStatus) as 'NotComplete',r.RegularID
FROM Booking b, Customer c
JOIN Regular r on r.RegularCID=c.CustomerID
WHERE c.CustomerID=b.BookingCustomerID

AND (b.BookingStatus='Pending'
or b.BookingStatus='OTW'
or b.BookingStatus='Started'
or b.BookingStatus='Unclaimed'
or b.BookingStatus='Confirmed')
GROUP by r.RegularID

最佳答案

您可以JOINRegular表,然后LEFT JOINBooking中的派生计数表> 表。我们这样做是为了避免对 Regular 表中的所有列进行 GROUP BY:

SELECT c.CustomerID, r.*, b.NotComplete
FROM Customer c
JOIN Regular r ON r.RegularCID = c.CustomerID
LEFT JOIN (SELECT BookingCustomerID, COUNT(*) AS NotComplete
FROM Booking
WHERE BookingStatus IN ('Pending', 'OTW', 'Started', 'Unclaimed', 'Confirmed')
GROUP BY BookingCustomerID) b ON b.BookingCustomerID = c.CustomerID

关于mysql - 合并并显示查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59100355/

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