Mysql 合并并显示查询结果

Mysql 合并并显示查询结果,mysql,sql,Mysql,Sql,我有两个问题: 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 Custome

我有两个问题:

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:
第一个问题

第二个问题

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

此外,还显示零(计数)

谢谢


这是我试了几个小时后得到的显然这不是我想要的

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   

常规
表上使用join,在第一个
选择时使用
子查询

SELECT t1.*, r.RegularCID FROM (
    SELECT CustomerID,count(b.BookingStatus) as 'NotComplete',
    FROM Booking b
    INNER JOIN Customer c ON c.CustomerID=b.BookingCustomerID
    WHERE (b.BookingStatus='Pending' OR b.BookingStatus='OTW')
    GROUP BY c.CustomerID) t1
LEFT JOIN Regular r on r.CustomerID = t1.CustomerID

您可以
JOIN
Regular
表,然后
LEFT JOIN
Booking
表中的派生计数表。我们这样做是为了避免
常规
表中的所有列进行分组:

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

每个
CustomerID
是否总是且只有一个
regulared
?请参考此内容。@Nick ya。。仅正则化perCustomerID@wayne9003不用担心-我很高兴我能帮上忙。如果一个客户ID有多个正则化呢?谢谢你的答复。thx@wayne9003您将在输出中获得多个行,每个
regulared
@wayne9003一行。可以修改查询以给出您需要的任何结果。@wayne9003这取决于您想要的结果。您想要单独的行,还是想要用逗号分隔的
正则值?
常规
表中的其他值如何?您想要它们吗?如果想要,您想要两组值吗?正如你所看到的,有很多变量。问一个新问题可能更容易。。。