还是问一个mysql的查询汇总的汇总问题

php中文网
发布: 2016-06-23 14:39:48
原创
1187人浏览过

本帖最后由 setoy 于 2013-12-18 18:11:02 编辑

有五个人,uid分别是1~5
有对着这五个人打分的表格(某个人有可能没有得到过分数)
id  uid  scroe-------------1    1    1.52    1    1.83    2    -5.54    1    0.85    2    8.26    1    -27    4    38    2    2.3--------------
登录后复制


现在要统计他们所有人的分数,即使查询不到某个人的得分,也要显示0分,显示格式如下:
用户  分数   汇总分-----------------------1      1.5     2.11      1.8     2.11      0.8     2.11      -2      2.12      -5.5    52      8.2     52      2.3     53      0       04      3       35      0       0
登录后复制

回复讨论(解决方案)

不知道楼主想要问什么

怎么得到第二张表,mysql语句怎么写?

建表:

CREATE TABLE a	(`id` int, `uid` int, `scroe` float);	INSERT INTO a	(`id`, `uid`, `scroe`)VALUES	(1, 1, 1.5),	(2, 1, 1.8),	(3, 2, -5.5),	(4, 1, 0.8),	(5, 2, 8.2),	(6, 1, -2),	(7, 4, 3),	(8, 2, 2.3);
登录后复制

问题是你数据集里都没uid=3和uid=5的纪录。参考

select t.*,round(s.scroe,1) as scroe , round(if(s.total is null,0,s.total),1) as summary from (select 1 as uid union select 2 as uidunionselect 3 as uid union select 4 as uidunion select 5 as uid) t left join (select uid,scroe,(select sum(scroe) from a where uid=aa.uid group by uid) as total from a as aa) son s.uid = t.uid
登录后复制

问题是你数据集里都没uid=3和uid=5的纪录。

uid是和user表里管理的字段,根据user表里的记录来读取:
在这里: http://bbs.csdn.net/topics/390673185


用户表tu

得分表t

SQL:SELECT tu.uid,t.score,a.total from test t right join test_user tu on tu.uid=t.uid left join (SELECT uid,sum(score) as total from test group by uid) a on a.uid=t.uid order by tu.uid

结果

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

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