This question already has answers here:
How can I return pivot table output in MySQL?
(8个答案)
3年前关闭。
我想从我的数据库桌上游戏中获取数据。
我搜索为
在游戏表中,我具有所有比赛的结果,例如
我所有想要获取的数据
如果有人知道我该怎么办?
(8个答案)
3年前关闭。
我想从我的数据库桌上游戏中获取数据。
我搜索为
SELECT team_id,result from games where team_id=17;
在游戏表中,我具有所有比赛的结果,例如
id team_id team result
21 17 India w
22 17 India l
23 17 India l
24 17 India w
我所有想要获取的数据
team_id result1 result2 result3 result4
17 w l l w
如果有人知道我该怎么办?
最佳答案
对于有限数量的结果(最多4个),您可以执行以下操作:
SELECT team_id,
MAX(CASE WHEN rn = 1 THEN result END) AS result1,
MAX(CASE WHEN rn = 2 THEN result END) AS result2,
MAX(CASE WHEN rn = 3 THEN result END) AS result3,
MAX(CASE WHEN rn = 4 THEN result END) AS result4
FROM (
SELECT team_id, result,
@row_number := @row_number + 1 AS rn
FROM games
CROSS JOIN (SELECT @row_number := 0) AS vars
WHERE team_id = 17
ORDER BY id) AS t
关于php - 如何在数据库搜索中的一行中获取数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37413670/