mySQL 多重 INNER JOIN
我们想要查询三个表,以找出每个企业的客户数量以及每个企业的记录数
这三个表是:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你需要这样的东西:
You need something like this:
如果您像这样添加一个
DISTINCT
怎么样:这样,对于每组businessDetails.name,您都可以在该“窗口”内获得不同clientDetails 和记录的计数。
How about if you add a
DISTINCT
like this:that way, for every group of businessDetails.name, you have the count of distinct clientDetails and records within that "window".
计数(不同[FieldName]) 在 MYSQL 中工作。尝试一下。
Count(Distinct [FieldName]) works in MYSQL. Give It a try.