Sql查询选择的最大数据组by

时间:2021-12-12 20:15:13

I have a problem with my query I have this data in table

我的查询有问题,数据在表中

id         id_game           score       level
1            2                1232        2
2            2                1234        2
3            2                234         3

I need to get data group by level but with the big score in the same moment My sql :

我需要逐级获取数据组,但同时我的sql也很出色:

select * from table where id_game = 2 
GROUP BY level 
ORDER BY level asc

This query return the id's : 1 and 3. I need 2 and 3 beceause score 1234 > 1232. Thx in advance and sorry for my english.

这个查询返回id: 1和3。我需要2和3,因为分数是1234 > 1232。Thx提前为我的英语道歉。

1 个解决方案

#1


4  

If you need the max you can use the proper aggregation function

如果需要最大值,可以使用合适的聚合函数

  select max(score), level 
  from table 
  group by level
  order by level asc

for the id

为id

then

然后

select * from table 
where (score, level) in (select max(score), level 
                         from table 
                        group by level) 

order by level 

#1


4  

If you need the max you can use the proper aggregation function

如果需要最大值,可以使用合适的聚合函数

  select max(score), level 
  from table 
  group by level
  order by level asc

for the id

为id

then

然后

select * from table 
where (score, level) in (select max(score), level 
                         from table 
                        group by level) 

order by level