我正在尝试为超过10位用户的公司找到已完成的操作计数以及公司详细信息。
我使用的代码是:
SELECT
c.company_name,
count(a.action) as actions,
count(u.user_id) as users
FROM
companies c
LEFT JOIN
auditing a on c.id=a.company_id
LEFT JOIN
users u on c.id=u.company_id
GROUP BY
c.id
HAVING
count(u.user_id)>10
LIMIT
10
当我运行它时,它显示的操作数量不正确。
例如,对于表中的第一家公司,它返回:
Company 1,111026,111026
然后应该返回:
Company 1,5093,22
我需要回复所有公司的行动数量和用户数量 - 我该怎么做?
答案 0 :(得分:2)
尝试输入DISTINCT,这样就不会计算重复数
SELECT
c.company_name,
count(DISTINCT a.action) as actions,
count(DISTINCT u.user_id) as users
FROM
companies c
LEFT JOIN
auditing a on c.id=a.company_id
LEFT JOIN
users u on c.id=u.company_id
GROUP BY
c.id
HAVING
count(u.user_id)>10
LIMIT
10