选择金额最高的记录

时间:2013-08-14 13:27:46

标签: mysql sql

您试图从db表中获取记录 根据比赛的最高比例

 user_id| game         | CompletePercentage
----------------------------------
 1      | name1        | 90
 2      | name2        | 67
 3      | name4        | 100
 4      | name1        | 50
 1      | name4        | 80
 1      | name3        | 70
 1      | name1        | 60
 2      | name2        | 70
 1      | name1        | 50

因此,如果我想获取所有已播放游戏的user_id 1记录,但只有最高

user_id | game         | highestPercent
----------------------------------
 1      | name1        | 90
 1      | name4        | 80
 1      | name3        | 70

我一直在尝试

SELECT user_id, game , MAX(CompletePercentage) AS highestPercent 
FROM completedscenarios 
GROUP BY game 
HAVING user_id=1

但成效有限 任何帮助表示赞赏。

6 个答案:

答案 0 :(得分:2)

SELECT user_id, game, CompletePercentage as highestPercentage
FROM completedscenarios 
WHERE user_id = 1 
ORDER BY CompletePercentage DESC
LIMIT 3

按百分比排序,MAX仅给出一个最大值。由于您希望按所需列添加多个记录顺序,并使用LIMIT修剪该集。

答案 1 :(得分:2)

您可以尝试像fiddle这样的自我加入:

为了扩展您可以使用它的其他一些方法,我已将原始小提琴更新为this。此外,我为用户4添加了一个额外的数据点,以显示重复项的内容。

  • 获取一个用户的数据(原始查询):
SELECT c1.*
FROM completedscenarios c1
  LEFT JOIN completedscenarios c2 
    ON c1.user_id = c2.user_id 
      AND c1.game = c2.game 
      AND c1.CompletePercentage < c2.completePercentage
WHERE c1.user_id = 1 and c2.user_id is null;
  • 获取所有用户及其每场比赛的最佳得分
SELECT c1.user_id, c1.game, c1.CompletePercentage
FROM completedscenarios c1
  LEFT JOIN completedscenarios c2 ON c1.user_id = c2.user_id 
    AND c1.game = c2.game 
    AND c1.CompletePercentage < c2.completePercentage
WHERE c2.user_id is null
ORDER BY c1.user_id, c1.CompletePercentage;
  • 无论用户是什么,都能获得所有游戏和最高百分比
SELECT c1.game, c1.user_id, c1.CompletePercentage
FROM completedscenarios c1
  LEFT JOIN completedscenarios c2 ON c1.game = c2.game 
    AND c1.CompletePercentage < c2.completePercentage
WHERE c2.game is null
ORDER BY c1.game, c1.CompletePercentage;
  • 获得用户及其最佳整体游戏
SELECT c1.user_id, c1.game, c1.CompletePercentage
FROM completedscenarios c1
  LEFT JOIN completedscenarios c2 ON c1.user_id = c2.user_id 
    AND c1.CompletePercentage < c2.completePercentage
WHERE c2.user_id is null
ORDER BY c1.user_id, c1.CompletePercentage;
  • 这是结果的副本
USER_ID | GAME  | COMPLETEPERCENTAGE
------------------------------------
1       | name1 | 90
1       | name4 | 80
1       | name3 | 70    
USER_ID | GAME  | COMPLETEPERCENTAGE
------------------------------------
1       | name3 | 70
1       | name4 | 80
1       | name1 | 90
2       | name2 | 70
3       | name4 | 100
4       | name1 | 50
4       | name2 | 70
GAME  | USER_ID | COMPLETEPERCENTAGE
------------------------------------
name1 | 1       | 90
name2 | 2       | 70
name2 | 4       | 70
name3 | 1       | 70
name4 | 3       | 100
    
USER_ID | GAME  | COMPLETEPERCENTAGE
------------------------------------
1       | name1 | 90
2       | name2 | 70
3       | name4 | 100
4       | name2 | 70

答案 2 :(得分:1)

SELECT  user_id,
        game,
        MAX(CompletePercentage) AS highestPercent
  FROM completedscenarios
  GROUP BY user_id, game
  WHERE user_id=1

答案 3 :(得分:0)

 SELECT user_id, game,CompletePercentage as highestPercent 
 FROM completedscenarios 
 WHERE user_id=1 
 ORDER BY highestPercent DESC
 Limit 5`

答案 4 :(得分:0)

按user_id分组,然后按游戏分组,最大(百分比)

select user_id, game,max(completepercentage) from @table
where user_id = 1
group by user_id,game

如果您愿意,可以添加排序,但上面给出了所需的结果

答案 5 :(得分:0)

此查询将帮助您获得每位用户百分比最高的游戏

SELECT g.user_id, g.game, g.CompletePercentage FROM 
games g 
INNER JOIN (
 SELECT user_id, game, CompletePercentage FROM games
 ORDER BY CompletePercentage DESC
) j ON g.user_id = j.user_id 
GROUP BY g.user_id 

在分组之前,这是一种众所周知的排序方法。