子查询 - 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. 主从备份 - 跪求mysql 高可用主从方案2. python - django 里自定义的 login 方法,如何使用 login_required()3. python如何不改动文件的情况下修改文件的 修改日期4. mysql主从 - 请教下mysql 主动-被动模式的双主配置 和 主从配置在应用上有什么区别?5. angular.js - 不适用其他构建工具,怎么搭建angular1项目6. android-studio - Android 动态壁纸LayoutParams问题7. mysql优化 - mysql count(id)查询速度如何优化?8. javascript - git clone 下来的项目 想在本地运行 npm run install 报错9. sql语句如何按or排序取出记录10. node.js - 使用 superagent 抓取 UTF-8网站乱码
