mysql - sql怎么查询分组后所有组一共有多少行数.
PHPz
PHPz 2017-04-17 13:36:25
[MySQL讨论组]

count(),只能统计每组有多少行,我现在想统计每组一共有多少行该怎么做?

SELECT COUNT(1) count,ac.chanceId,act.quantity,act.fullScale
FROM actgamechance ac 
LEFT JOIN actredpacketrain act ON ac.rainId = act.rainId 
WHERE ac.chanceState = 0 AND (NOW()<=ac.expireTime AND NOW()>=ac.effectiveTtime) 
AND ac.userInfoId = '001ecc591eeb4ee180a35b29da89b395'
GROUP BY ac.sources
ORDER BY act.rainLevel DESC
FOR UPDATE

查询为

2 1 50 50
2 3 50 50

变为

2 1 50 50 4(所有的count相加)
2 3 50 50 4(所有的count相加)

怎么实现?

PHPz
PHPz

学习是最好的投资!

全部回复(1)
迷茫

尝试使用
http://segmentfault.com/q/1010000004152762

select
c, chanceid, quantity, fullscale, 
@total := @total + c as s
from (
SELECT COUNT(1) count,ac.chanceId,act.quantity,act.fullScale
FROM actgamechance ac 
LEFT JOIN actredpacketrain act ON ac.rainId = act.rainId 
WHERE ac.chanceState = 0 AND (NOW()<=ac.expireTime AND NOW()>=ac.effectiveTtime) 
AND ac.userInfoId = '001ecc591eeb4ee180a35b29da89b395'
GROUP BY ac.sources
) as j, (select @total := 0) as t
ORDER BY act.rainLevel DESC
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号