子查询 - 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. php study 安装 composer 用不了2. 零基础的新手小白想学PHP开发应该从哪儿开始学起来3. 用命令无法登陆mysql页面求解答4. 给变量赋值的时候是不是也要加//5. 创建数据表错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL serve6. 源代码打包APP 求过程7. 求救一下,用新版的phpstudy,数据库过段时间会消失是什么情况?8. javascript - 有没有什么办法过滤掉数据库中的乱码?9. spring - mysql中事务是互斥的么?10. 微信渠道二维码怎么使用?

网公网安备