如圖是我篩選出來(lái)的數(shù)據(jù),語(yǔ)句是
select time,wish_num,num from wish_num where time >= '15296000' and time <= '1495382399' group by time,wish_num,
time和wish_num是聯(lián)合主鍵
現(xiàn)在我希望把同一個(gè)日期中的資料合併成一行,如
日期1次2次5次10次20次
1495294000 2 2 4 11 2
1495296000 2 2 4 11 2 、
形如這樣的格式,請(qǐng)問(wèn)要怎麼修改上面的語(yǔ)句,進(jìn)行子查詢(xún)還是?
最簡(jiǎn)單就是group_concat了,樓主不用那就只好case when了,由於樓主group by之後的num並沒(méi)有使用聚合函數(shù),因此我理解為num只有一個(gè)值? 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;