我有一个表,其中包含 id、tid、companyid、ttype 等列。
许多公司ID的ID可能相同,但在公司ID内是唯一的,并且 tid 始终是唯一的,并且
我想计算表格中输入的交易总数,
单个事务可以插入多行,
例如,
id tid companyid ttype
1 1 1 xxx
1 2 1 may be null
2 3 1 yyy
2 4 1 may be null
2 5 1 may be null
以上条目应仅计为 2 笔交易..
它可能会重复许多 companyids..
那么我如何计算表格中输入的交易总数
我试过了
select sum(count(*)) from transaction group by id,companyId;
但是没用
select count(*) from transaction group by id;
不会工作,因为不同的公司 ID 可能会重复 ID。
最佳答案
试试这个
SELECT SUM(s.counts) transactions FROM (SELECT COUNT(*) counts FROM transaction GROUP BY id,companyId) s;
关于mysql - 想计算group by option渲染的count的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2590498/