子查詢 - mysql如何把多行數(shù)據(jù)合并到一行的多列中
問題描述
如圖是我篩選出來的數(shù)據(jù),語句是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)在我希望把同一個日期中的數(shù)據(jù)合并成一行,如日期 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并沒有使用聚合函數(shù),因此我理解為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;
相關(guān)文章:
1. php - 微信開發(fā)驗證服務(wù)器有效性2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫過段時間會消失是什么情況?3. javascript - 我的站點貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?4. mysql - 請問數(shù)據(jù)庫字段為年月日,傳進的參數(shù)為月,怎么查詢那個月所對應的數(shù)據(jù)5. Python2中code.co_kwonlyargcount的等效寫法6. python - 如何判斷字符串為企業(yè)注冊名稱7. html - 移動端radio無法選中8. javascript - vue+iview upload傳參失敗 跨域問題后臺已經(jīng)解決 仍然報403,這是怎么回事啊?9. [python2]local variable referenced before assignment問題10. python中怎么對列表以區(qū)間進行統(tǒng)計?
