需求:有多个游戏区服,现在要把他们合并到一个服,问题是每个区的关卡都有一个排名第一的,合服后要在多个第一中选出一个作为合服后的第一,选择条件是 关卡最高得分.
一句sql搞定,代码如下:
SELECT * FROM (
SELECT *,(
SELECT B.id FROM (SELECT * FROM tower.`stagerank` UNION ALL SELECT * FROM tower_cy.`stagerank`) B
WHERE A.id=B.id AND A.highScore > B.highScore
) AS objFROM (SELECT * FROM tower.`stagerank` UNION ALL SELECT * FROM tower_cy.`stagerank`) A ORDER BY id , highScore DESC
) T WHERE obj IS NOT NULL