我需要獲得第一和最高分數爲每個game_id中列轉換多個行,每課:SQL:在同一行
--------------------------------------------
|id |lesson_id |game_id |score |date |
--------------------------------------------
|1 |1 |0 |20 |1391627323 |
|2 |1 |0 |80 |1391627400 |
|3 |1 |1 |5 |1391627543 |
|4 |1 |2 |7 |1391627450 |
|5 |2 |0 |90 |1391627323 |
|6 |2 |1 |10 |1391628000 |
|7 |2 |2 |8 |1391628005 |
|8 |2 |2 |9 |1391628010 |
|9 |2 |0 |95 |1391628333 |
|10 |3 |0 |50 |1391627323 |
--------------------------------------------
我需要這樣的輸出:
---------------------------------------------------
| |game_id = 0 |game_id = 1 |game_id = 2 |
|lesson_id |first |max |first |max |first |max |
---------------------------------------------------
|1 |20 |80 |5 |5 |7 |7 |
|2 |90 |95 |10 |10 |8 |9 |
|3 |50 |50 |- |- |- |- |
---------------------------------------------------
到目前爲止,我有這個,它得到一排每個game_id(第一和max),但很明顯,這些值必須在同一行中:
SELECT game_id, lesson_id, MAX(score) AS max_score, MIN(date), score AS first_score FROM cdu_user_progress
GROUP BY game_id
任何人提供任何幫助?
看起來你需要PIVOT命令。請參閱http://www.codeproject.com/Tips/500811/Simple-Way-To-Use-Pivot-In-SQL-Query – mockaroodev
您使用的是哪個數據庫? –
我正在使用MySQL – user3241112