在 group_concat 中添加第二列以按 mysql 排序
Add second column to order by mysql in a group_concat
我根据此处其他人的问题提出了这个查询。
SELECT *, FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score ASC) FROM EventPlayerResult WHERE eventId = 'EventTest0') ) AS position FROM EventPlayerResult WHERE eventId = 'EventTest0' ORDER BY position ASC LIMIT 10
它为我提供了前 10 名玩家的排行榜。但是当我 运行 它时,如果 2 个玩家的分数相同,我需要它通过另一列 (energyLeft) 进行过滤。所以我尝试在我的 GROUP_CCONCAT 中添加 energyLeft DESC 但它没有改变任何东西。我不熟悉 group concat 和 find in set。那么我应该在按分数排序后在何处添加按energyLeft排序的逻辑呢?
我试过这样的事情:
SELECT *, FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score ASC, energyLeft DESC) FROM EventPlayerResult WHERE eventId = 'EventTest0') ) AS position FROM EventPlayerResult WHERE eventId = 'EventTest0' ORDER BY position ASC LIMIT 10
您应该使用玩家而不是 GROUP_CONCAT()
中的分数,以便玩家按 score
排名,然后按 energyLeft
排名。
假设 table 中有像 player_id
这样的列:
SELECT *,
FIND_IN_SET(
player_id,
(
SELECT GROUP_CONCAT(player_id ORDER BY score ASC, energyLeft DESC) FROM EventPlayerResult WHERE eventId = 'EventTest0'
)
) AS position
FROM EventPlayerResult
WHERE eventId = 'EventTest0'
ORDER BY position ASC LIMIT 10;
我根据此处其他人的问题提出了这个查询。
SELECT *, FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score ASC) FROM EventPlayerResult WHERE eventId = 'EventTest0') ) AS position FROM EventPlayerResult WHERE eventId = 'EventTest0' ORDER BY position ASC LIMIT 10
它为我提供了前 10 名玩家的排行榜。但是当我 运行 它时,如果 2 个玩家的分数相同,我需要它通过另一列 (energyLeft) 进行过滤。所以我尝试在我的 GROUP_CCONCAT 中添加 energyLeft DESC 但它没有改变任何东西。我不熟悉 group concat 和 find in set。那么我应该在按分数排序后在何处添加按energyLeft排序的逻辑呢?
我试过这样的事情:
SELECT *, FIND_IN_SET( score, ( SELECT GROUP_CONCAT( score ORDER BY score ASC, energyLeft DESC) FROM EventPlayerResult WHERE eventId = 'EventTest0') ) AS position FROM EventPlayerResult WHERE eventId = 'EventTest0' ORDER BY position ASC LIMIT 10
您应该使用玩家而不是 GROUP_CONCAT()
中的分数,以便玩家按 score
排名,然后按 energyLeft
排名。
假设 table 中有像 player_id
这样的列:
SELECT *,
FIND_IN_SET(
player_id,
(
SELECT GROUP_CONCAT(player_id ORDER BY score ASC, energyLeft DESC) FROM EventPlayerResult WHERE eventId = 'EventTest0'
)
) AS position
FROM EventPlayerResult
WHERE eventId = 'EventTest0'
ORDER BY position ASC LIMIT 10;