两个带有左连接的 COUNTS 相乘

发布于 2024-11-15 21:06:12 字数 438 浏览 5 评论 0原文

我正在尝试获取一个查询,该查询为我提供表 Users 中的用户名、用户从表 Job2User 中拥有的作业数以及用户从表 Place2User 中拥有的地点数。

以下查询不断将两个计数列的值相乘。例如,如果对于用户 1,作业计数应为 2,位置计数应为 4,则用户 1 行中的两列都将显示“8”。我不确定我做错了什么:

SELECT `UserName`, COUNT(`Job2User`.`UserID`), COUNT(`Place2User`.`UserID`) 
FROM `Users`
LEFT JOIN `Job2User` ON `Job2User`.`UserID`=`Users`.`UserID` 
LEFT JOIN `Place2User` ON `Place2User`.`UserID`=`Users`.`UserID` 
GROUP BY `UserName`;

I am trying to get a query that gives me the UserNames from table Users, the number of Jobs that user has from table Job2User, and the number of Places that user has from the table Place2User.

The following query keeps multiplying the value of the two count columns. For example, if for User 1 the Jobs count should be 2 and the Places count should be 4, both columns in the User 1 row will display "8". I'm not sure what I'm doing wrong:

SELECT `UserName`, COUNT(`Job2User`.`UserID`), COUNT(`Place2User`.`UserID`) 
FROM `Users`
LEFT JOIN `Job2User` ON `Job2User`.`UserID`=`Users`.`UserID` 
LEFT JOIN `Place2User` ON `Place2User`.`UserID`=`Users`.`UserID` 
GROUP BY `UserName`;

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

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

发布评论

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

评论(5

素罗衫 2024-11-22 21:06:12

您应该使用 count(distinct ...) 来计算唯一值。不是对 userid(外键)进行计数,而是对引用表的主键进行计数。

参阅此处的文档

请 得到 8 个,因为您从作业返回 2 条记录,从地点返回 4 条记录。由于您没有计算不同的值,因此您得到 2*4 = 8。

You should use count( distinct ...) to count unique values. Instead of counting userid ( the foreign key) count the referenced table's primary key.

See the docs here

You are getting eight because you are returning 2 records from jobs and 4 from places. Since you are not counting distinct values you get 2*4 = 8.

路弥 2024-11-22 21:06:12

您的问题可能是您没有映射 Place2User 和 Job2User 表,因此您正在执行交叉联接。 有关交叉联接的详细信息

您将需要使用内部查询来实现此目的除非您这两个表 Place2User 以某种方式与 Job2User 相关。

试试这个:

SELECT `UserName`, `Job2User`.`Count`, `Place2User`.`Count` 
FROM `Users`
LEFT JOIN (SELECT `UserID`, COUNT(1) AS 'Count' FROM `Job2User` GROUP BY `UserID`) `Job2User` ON `Job2User`.`UserID`=`Users`.`UserID` 
LEFT JOIN (SELECT `UserID`, COUNT(1) AS 'Count' FROM `Place2User` GROUP BY `UserID`) `Place2User` ON `Place2User`.`UserID`=`Users`.`UserID` 

Your problem is likely that you're not mapping Place2User and Job2User tables, thus you are preforming a cross join. More info on Cross Joins

You will need to use inner queries to achieve this unless you the two tables Place2User relates to the Job2User in some way.

Try this:

SELECT `UserName`, `Job2User`.`Count`, `Place2User`.`Count` 
FROM `Users`
LEFT JOIN (SELECT `UserID`, COUNT(1) AS 'Count' FROM `Job2User` GROUP BY `UserID`) `Job2User` ON `Job2User`.`UserID`=`Users`.`UserID` 
LEFT JOIN (SELECT `UserID`, COUNT(1) AS 'Count' FROM `Place2User` GROUP BY `UserID`) `Place2User` ON `Place2User`.`UserID`=`Users`.`UserID` 
执手闯天涯 2024-11-22 21:06:12

一种选择是对要计数的每个表使用内联视图

SELECT `UserName`, `Job2User`.`Job2UserCount`, `Place2User`.`Place2UserCount`
FROM `Users`
 ` 
LEFT JOIN
(SELECT

 COUNT(`Job2User`.`UserID`) Job2UserCount ,
 UserID
FROM
 Job2User
 GROUP BY 
  `UserID` )Job2User
ON `Job2User`.`UserID`=`Users`.`UserID
LEFT JOIN
(SELECT

 COUNT(`Place2User`.`UserID`) Place2UserCount,
 UserID
FROM
 Job2User
 GROUP BY 
  `UserID` )Place2User
ON `Place2User`.`UserID`=`Users`.`UserID` 
GROUP BY `UserName`; 

One option is to use inline views for each table you want to count

SELECT `UserName`, `Job2User`.`Job2UserCount`, `Place2User`.`Place2UserCount`
FROM `Users`
 ` 
LEFT JOIN
(SELECT

 COUNT(`Job2User`.`UserID`) Job2UserCount ,
 UserID
FROM
 Job2User
 GROUP BY 
  `UserID` )Job2User
ON `Job2User`.`UserID`=`Users`.`UserID
LEFT JOIN
(SELECT

 COUNT(`Place2User`.`UserID`) Place2UserCount,
 UserID
FROM
 Job2User
 GROUP BY 
  `UserID` )Place2User
ON `Place2User`.`UserID`=`Users`.`UserID` 
GROUP BY `UserName`; 
暮色兮凉城 2024-11-22 21:06:12

我看到的第一个问题是你的连接向后。您正在进行左联接,但用户表位于该联接的右侧。您需要像这样进行切换:

SELECT `UserName`, COUNT(`Job2User`.`UserID`), COUNT(`Place2User`.`UserID`) 
FROM `Users`
LEFT JOIN `Job2User` ON `Users`.`UserID`=`Job2User`.`UserID` 
LEFT JOIN `Place2User` ON `Users`.`UserID` =`Place2User`.`UserID`
GROUP BY `UserName`;

由于您正在对未链接时将为 NULL 的字段进行计数,因此这可能会起作用(抱歉已经晚了,我的大脑有点模糊)。如果没有,您可以采用以下方法:

SELECT UserName, 
  (SELECT COUNT(Job2User.UserID) 
   FROM Job2User 
   WHERE Job2User.UserID = t.UserID) AS Job2UserCount,
  (SELECT COUNT(Place2User.UserID) 
   FROM Place2User 
   WHERE Place2User.UserID = t.UserID) AS Place2UserCount
FROM Users AS t

The first issue I see is that you have the joins backwards. You are doing left joins but the Users table is on the right of that join. You need to switch that around like so:

SELECT `UserName`, COUNT(`Job2User`.`UserID`), COUNT(`Place2User`.`UserID`) 
FROM `Users`
LEFT JOIN `Job2User` ON `Users`.`UserID`=`Job2User`.`UserID` 
LEFT JOIN `Place2User` ON `Users`.`UserID` =`Place2User`.`UserID`
GROUP BY `UserName`;

Since you are doing the count on the fields that will be NULL when they aren't linked, this might work (sorry it is late and my brain is a little fuzzy). If it doesn't, here is a way you could do it:

SELECT UserName, 
  (SELECT COUNT(Job2User.UserID) 
   FROM Job2User 
   WHERE Job2User.UserID = t.UserID) AS Job2UserCount,
  (SELECT COUNT(Place2User.UserID) 
   FROM Place2User 
   WHERE Place2User.UserID = t.UserID) AS Place2UserCount
FROM Users AS t
花桑 2024-11-22 21:06:12

好吧,对于每个 Job2User 来说,获取所有 Place2user 很简单,这就是为什么它会成倍增加。你应该在子查询中计算它然后使用左连接。

Well its simple for each Job2User ure getting all Place2user its why its multipyed. U should count it in subquery then use left join.

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