mysql - 如何在一个sql中执行两个不同的计数

标签 mysql sql

如何在一个表中进行两个不同的计数? 两个不同的计数函数对不同的列进行计数。

简化表:

id,creatorId,resolverId
'1','1','2'
'2','1','2'
'3','2','2'
'4','2','1'

我想要做的是将creatorId,COUNT(creatorId),resolverId,COUNT(resolverId)放入一张表中。喜欢:

creatorId,COUNT(creatorId),resolverId,COUNT(resolverId)
'1','2','1','1'
'2','2','2','3'

我只通过了使用UNION将它们放入2列的测试,并且我尝试了JOIN,但它对MySQL来说是非法的。

SELECT creatorId, COUNT(creatorId)
FROM issue AS a 
GROUP BY creatorId
join(
SELECT resolverId, COUNT(resolverId) 
FROM issue AS b
GROUP BY resolverId)
WHERE a.creatorId = b.resolverId;

错误信息是:

错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 4 行 0.00034 秒的“join( SELECT resolverId, COUNT(resolverId) FROM issues AS b GROUP BY resolverId)”附近使用的正确语法

谁能告诉我该如何处理?或者给我一个例子? 谢谢!

最佳答案

select  a.creatorId,COUNT(a.creatorId), t.resolverId, count_resolved_id
from issue a
inner join (
  SELECT b.resolverId, COUNT(b.resolverId)  count_resolved_id
  FROM issue AS b
  GROUP BY resolverId
) t on t.resolverId = a.creatorId
group by a.creatorId;

关于mysql - 如何在一个sql中执行两个不同的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470880/

相关文章:

php - 限制保存到其他表的数据数量

php - 我的值(value)为 2018-05-03 11 :02:00 in mysql i want to pass this in my where condition using php

mysql排除查询

mysql - Importing 1GO SQL File => ERROR 2013 (HY000) at line 23 : Lost connection to MySQL server during query

sql - NOT IN 不在 google BigQuery 标准 sql 中工作

mysql - 多态关联替代

sql - 规范化 MySQL 查询中的重音字符

mysql - 具有额外信息的多对多关系

java - 尝试使用多个表时 SQLite 数据库错误

python - 在应用于列 : works on one install, 而不是其他列的函数上创建索引