我有两个表,一个包含分数,另一个包含游戏列表。游戏有不同类型的高分,例如高尔夫比赛是由谁得分最低的。
我试图列出某个用户与其他用户相比的高分(包括将高分视为最低分的游戏。)
我的旧查询只是假设所有分数都是MAX分数,但现在我需要能够检查游戏的分数类型是否为“低”。我听说使用CASE可以解决这个问题,但我正试图碰到游戏桌。
SELECT s.game_id, fp.score FROM game_scores s INNER JOIN games g ON s.game_id = g.id INNER JOIN ( SELECT CASE WHEN g.score_type = low THEN MIN(score) WHEN g.score_type = high THEN MAX(score) END as score, game_id FROM game_scores GROUP BY game_id ) fp ON s.game_id = fp.game_id AND s.score = fp.score WHERE s.user_id = 1
答案 0 :(得分:0)
所以,为了得到更好的答案,我会根据我的建议发布所有代码。
SELECT s.game_id, fp.score
FROM game_scores s
INNER JOIN games g ON s.game_id = g.id
INNER JOIN (
SELECT
CASE
WHEN g.score_type = low
THEN MIN(score)
ELSE MAX(score)
END as score,
game_id
FROM game_scores
GROUP BY game_id
) fp
ON s.game_id = fp.game_id AND s.score = fp.score
WHERE s.user_id = 1
答案 1 :(得分:0)
SELECT
s.game_id,
CASE
WHEN g.score_type = low THEN MIN(s.score)
WHEN g.score_type = high THEN MAX(s.score)
END as score
FROM game_scores s
INNER JOIN games g ON s.game_id = g.id
WHERE s.user_id = 1
GROUP BY s.game_id, g.score_type