mySQL 多重 INNER JOIN

发布于 2024-10-08 08:58:57 字数 1833 浏览 5 评论 0原文

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

这三个表是:

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

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

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

我们可以从其中两个表中选择计数,这没有任何问题一次(businessDetails 加上 clientDetails 或记录)。例如,

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 和记录进行计数。我们尝试了以下查询,但发现它由于某种原因使总客户端数成倍增加:

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

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

We have three tables that we would like to query to find out the count of clients per business and the number of records per business

The three tables are:

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

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

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

We don't have any problem with being able to select a count from two of the tables at one time (businessDetails plus either clientDetails OR records). For example

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

This query gives us a nice result as expected:

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

The problem we are running into is how to do a count on both clientDetails AND records at once. We tried the following query, but have found that it multiplies the number of totalClients for some reason:

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

This returns a result something like:

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

I expect we're just making some simple error. Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〃温暖了心ぐ 2024-10-15 08:58:57

你需要这样的东西:

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

You need something like this:

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
在梵高的星空下 2024-10-15 08:58:57

如果您像这样添加一个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 和记录的计数。

How about if you add a DISTINCT like this:

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

that way, for every group of businessDetails.name, you have the count of distinct clientDetails and records within that "window".

一人独醉 2024-10-15 08:58:57

计数(不同[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

Count(Distinct [FieldName]) works in MYSQL. Give It a try.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文