Mysql获取值为max的整行

时间:2015-12-09 19:31:23

标签: mysql

我正在尝试以下

Table : teams_members

Id          Team_id          User_id

1           103              65524
14          103              65529
16          103              65530
17          103              65526
19          103              65528

Table : users
Id          username

65524       Kaiprioska
65526       Darkstarling 
65528       Metazoans98
65529       Darth Laser
65530       TheBourbonKid

Table : users_playeraccounts
Id       user_id      summoner_name         rank_solo

7        65524        kaiprioska            19 
8        65524        swaggapandaa          14
9        65524        Yolonese Prophet      12
10       65529        Darth Laser           15
11       65530        TheBourbonkid         18
12       65528        Metazoans98           9
13       65530        TheBourbonKid         12

结果需要:

user_id  summoner_name    rank_solo

65524    kaiprioska       19
65529    Darth laser      15
65530    TheBourbonkid    18
65528    metazoans98      9
655529   darth laser      15

所以每个团队成员我都需要users_playeraccounts条目,其中rank_solo是该user_id的最大值。我可以得到完整的清单,以便每个成员都能获得每个成员,但不是最大入口。

非常感谢任何协助。

4 个答案:

答案 0 :(得分:0)

select user_id, summoner_name, max(rank_solo ) as rank_solo
    from users_playeraccounts
    group by user_id

答案 1 :(得分:0)

试试这个

SELECT USER_ID, SUMMONER_NAME, MAX(RANK_SOLO) FROM users_playeraccounts GROUP BY USER_ID

答案 2 :(得分:0)

你需要两次查询表格;一次获得最高级别,另一次获取它们的记录。 (顺便说一句,如果用户有两个最高级别,则两者都在结果中。)

SELECT q.user_id, q.summoner_name, q.rank_solo 
FROM (
   SELECT user_id, MAX(rank_solo) AS rank_solo
   FROM users_playeraccounts 
   GROUP BY user_id
) As maxQ
INNER JOIN users_playeraccounts AS q USING (user_id, rank_solo)
GROUP BY q.user_id -- If you only want one per user.
;

答案 3 :(得分:0)

SELECT u.id,u.username,solo_rank FROM(     SELECT [user_id] userID,MAX(rank_solo)solo_rank FROM users_playeraccounts GROUP BY [user_id] )BestSoloRanking 加入用户u ON BestSoloRanking.userID = u.id