gpt4 book ai didi

MySQL如何从GROUP BY中计数

转载 作者:行者123 更新时间:2023-11-29 12:45:53 25 4
gpt4 key购买 nike

date    | userid | companyid  
12.8.14 | 1 | 1
12.8.14 | 2 | 2
12.8.14 | 3 | 1

我有一张像上面这样的 table 。通过正常查询,很容易从表中统计出有多少家公司1

我的问题是:如果我的查询是select * from table where companyid = '1' group by date,我怎样才能让公司的mysql_num_row等于21userid13

select * from table where companyid = '1' group by date 只会返回我mysql_num_row 等于 1 且结果 12.8.14 | 1 | 1

最佳答案

您可以嵌套查询来获取第一家公司条目的总和,然后将该嵌套查询连接到外部查询:

SELECT ABB2.*, ABB1.mysql_num_row
FROM `table` AS ABB2
JOIN
(SELECT companyid, COUNT(userid) AS mysql_num_row
FROM `table`
GROUP BY companyid) AS ABB1 ON ABB1.companyid = ABB2.companyid
WHERE ABB2.companyid = 1;

Example

关于MySQL如何从GROUP BY中计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25439456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com