繁体   English   中英

MySQL如何从GROUP BY进行计数

[英]MySQL how to count from GROUP BY

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

我有一张上面的桌子。 通过正常查询,很容易从表中计算出多少个公司1

我的问题是:如果我的查询是select * from table where companyid = '1' group by date ,那么我如何才能得到mysql_num_row等于2的公司1userid13

select * from table where companyid = '1' group by date只会返回mysql_num_row等于1并返回12.8.14 | 1 | 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;

也尝试这样

select *,(select count(*) from table1 where companyid=a.companyid) as count 
from t  as a where companyid=1

你自找的:

select date,companyid,count(*)
from table
where userid = 1
group by date,companyid

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM