mySQL多个INNER JOIN

时间:2010-12-21 19:17:58

标签: mysql count inner-join

我们想要查询三个表,以查找每个企业的客户数和每个企业的记录数

这三个表是:

businessDetails
-------------------
businessDetails.ID
businessDetails.name

clientDetails
-------------------
clientDetails.ID
clientDetails.businessDetailsID

records
-------------------
records.ID
records.businessDetailsID

我们没有任何问题能够一次从两个表中选择一个计数(businessDetails加上clientDetails OR记录)。例如

SELECT  businessDetails.name AS businessName
        , COUNT(clientDetails.businessDetailsID) AS totalClients
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC

此查询为我们提供了预期的良好结果:

--------------------------------
businessName    |   totalClients
--------------------------------
Initech         |   23
Cylon Inc       |   148
The Dude Ltd    |   71

我们遇到的问题是如何同时对clientDetails AND记录进行计数。我们尝试了以下查询,但发现由于某种原因它会使totalClients的数量成倍增加:

SELECT  businessDetails.name AS businessName
        , COUNT(clientDetails.businessDetailsID) AS totalClients
        , COUNT(records.businessDetailsID) AS totalRecords
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC

这会返回如下结果:

--------------------------------------------------------
businessName    |   totalClients    |   totalRecords
--------------------------------------------------------
Initech         |   93              |    93
Cylon Inc       |   398             |    398
The Dude Ltd    |   215             |    215

我希望我们只是犯了一些简单的错误。任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:4)

你需要这样的东西:

SELECT  
    businessDetails.name AS businessName,
    (SELECT count('x') FROM clientDetails WHERE clientDetails.businessDetailsID = businessDetails.businessDetailsID) as totalclients,
    COUNT(records.businessDetailsID) AS totalRecords
FROM 
    `businessDetails`
    INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
    businessDetails.name
ORDER BY
    totalClients DESC

答案 1 :(得分:2)

如果你这样添加DISTINCT怎么样:

SELECT  businessDetails.name AS businessName
        , COUNT(distinct clientDetails.ID) AS totalClients
        , COUNT(distinct records.ID) AS totalRecords
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC

这样,对于每个businessDetails.name组,您在该“窗口”中拥有不同clientDetails和记录的计数。

答案 2 :(得分:1)

Count(Distinct [FieldName])适用于MYSQL。试一试。

SELECT  businessDetails.name AS businessName
        , COUNT(Distinct clientDetails.businessDetailsID) AS totalClients
        , COUNT(records.businessDetailsID) AS totalRecords
FROM `businessDetails`
INNER JOIN clientDetails 
        ON clientDetails.businessDetailsID = businessDetails.businessDetailsID
INNER JOIN records ON records.businessDetailsID = businessDetails.ID
GROUP BY 
businessDetails.name
ORDER BY
totalClients DESC