我有这样的疑问:

SELECT business.bussId, COUNT(invoices.userId) as invoices, COUNT(rating.bussId) as ratingCount ,
       FROM business
       LEFT JOIN invoice ON (invoice.bussId = business.bussId AND invoice.userId = '3000' )
       LEFT JOIN rating ON (rating.bussId = business.bussId )
       WHERE business.bussId=100

COUNT(invoices.userId)用户应该返回invoices表中的行数,其中userId=3000,bussId=100。
换句话说,这个查询:
SELECT COUNT(*) as invoice FROM `invoices` WHERE bussId = '100' AND userId = '30000'.

第二个查询返回COUNT(*)=3,第一个查询(当用户离开JOIN时)返回15,如何修复?

最佳答案

尝试以下查询:

SELECT business.bussId, (select count(invoices.userId) from invoice where invoice.bussId = a.bussId AND invoice.userId = '3000' ) as invoices, (select COUNT(rating.bussId) from rating where rating.bussId = a.bussId ) as ratingCount ,
       FROM business a
              WHERE business.bussId=100

09-11 19:18