子查询 - mysql如何把多行数据合并到一行的多列中
问题描述
如图是我筛选出来的数据,语句是select time,wish_num,num from wish_num where time >= ’15296000’ and time <= ’1495382399’ group by time,wish_num,time和wish_num是联合主键现在我希望把同一个日期中的数据合并成一行,如日期 1次 2次 5次 10次 20次1495294000 2 2 4 11 2 1495296000 2 2 4 11 2 、形如这样的格式,请问要怎么修改上面的语句,进行子查询还是?
问题解答
回答1:最简单就是group_concat了,楼主不用那就只好case when了,由于楼主group by之后的num并没有使用聚合函数,因此我理解为num只有一个值?sql如下
select time,max(case when wish_num=1 then num else 0) ’1’,max(case when wish_num=2 then num else 0) ’2’,max(case when wish_num=5 then num else 0) ’5’,max(case when wish_num=10 then num else 0) ’10’,max(case when wish_num=20 then num else 0) ’20’from wish_num where time >= ’15296000’ and time <= ’1495382399’ group by time;
相关文章:
1. dockerfile - [docker build image失败- npm install]2. docker gitlab 如何git clone?3. docker网络端口映射,没有方便点的操作方法么?4. docker api 开发的端口怎么获取?5. docker-compose中volumes的问题6. boot2docker无法启动7. 关docker hub上有些镜像的tag被标记““This image has vulnerabilities””8. dockerfile - 我用docker build的时候出现下边问题 麻烦帮我看一下9. docker不显示端口映射呢?10. debian - docker依赖的aufs-tools源码哪里可以找到啊?

网公网安备