1

嗨,尝试根据游戏的最高百分比从数据库表中获取记录

 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

但只取得了有限的成功。
任何帮助表示赞赏。

4

6 回答 6

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

按百分比排序,MAX只给出一个最大值。由于您希望按列排列多个记录顺序并用于LIMIT修剪集合。

于 2013-08-14T13:30:40.147 回答
2

您可以尝试像这个小提琴这样的自我加入:

只是为了扩展您可以使用它的其他一些方式,我已将原始小提琴更新为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
于 2013-08-14T13:39:16.220 回答
1
SELECT  user_id,
        game,
        MAX(CompletePercentage) AS highestPercent
  FROM completedscenarios
  GROUP BY user_id, game
  WHERE user_id=1
于 2013-08-14T13:30:39.327 回答
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 

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

于 2013-08-14T14:22:33.990 回答
0
 SELECT user_id, game,CompletePercentage as highestPercent 
 FROM completedscenarios 
 WHERE user_id=1 
 ORDER BY highestPercent DESC
 Limit 5`
于 2013-08-14T13:31:19.113 回答
0

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

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

然后,您可以根据需要添加排序,但以上给出了所需的结果

于 2013-08-14T13:33:16.843 回答