我有一个用PHP编写的SQL请求,我想优化它。
我认为这是可能的,因为一切都在同一张桌子里。我不是SQL专家,我搜索的关键字包括“分享”和“结果”/“子”/“部分”没有帮助。
SELECT C0.bestScore, C1.playerScore, C2.playerRank FROM
(SELECT MAX(score) AS bestScore FROM `{$l_tableName}`) C0,
(SELECT MAX(score) AS playerScore FROM `{$l_tableName}` WHERE player_id = {$l_playerId}) C1,
(SELECT COUNT(*)+1 AS playerRank FROM `{$l_tableName}` WHERE score > (SELECT MAX(score) FROM `{$l_tableName}` WHERE player_id = {$l_playerId}) ) C2;
由于
答案 0 :(得分:1)
你可以用这个来清理前两个,不完全确定第三部分,你不清楚你在追求什么,分数怎么能大于同一张桌子的最高分?
SELECT MAX(score) AS bestScore
, MAX(CASE WHEN player_id = {$l_playerId}) THEN score ELSE '' END) AS playerScore
FROM `{$l_tableName}`