mysql 行轉列查詢 用於統計

表結構:


行轉列查詢:


SELECT 

user_id,
(SELECT COUNT(*) FROM daili_workcount AS t1 WHERE `type`=1  AND t1.user_id=c.user_id) as type1,
(SELECT COUNT(*) FROM daili_workcount  AS t2 WHERE `type`=2  AND t2.user_id=c.user_id) as type2,
(SELECT COUNT(*) FROM daili_workcount  AS t3 WHERE `type`=3  AND t3.user_id=c.user_id) as type3,
(SELECT COUNT(*) FROM daili_workcount  AS t4 WHERE `type`=4  AND t4.user_id=c.user_id) as type4,
(SELECT COUNT(*) FROM daili_workcount  AS t5 WHERE `type`=5  AND t5.user_id=c.user_id) as type5

 FROM daili_workcount as c   group by user_id

查詢結果:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章