Mysql 需要帮助创建SQL语句吗
这是表格数据Mysql 需要帮助创建SQL语句吗,mysql,sql,Mysql,Sql,这是表格数据 Name Stage ID Event ID Attempt No Score Ramesh 1 1 1 10 Ramesh 1 1 2 20 Ramesh 2 1 1
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 1 10
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 1 25
Suresh 2 2 2 35
Suresh 2 2 3 30
我们有一个包含名称、阶段ID、事件ID、尝试编号和分数的表
我们要将数据分组为名称、阶段ID、事件ID、尝试号和MaxScore
每个阶段、事件和名称下的每次尝试都会有记录,我们需要取最大分数,同样需要显示
输出应为:
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 2 35
这将有一条name+stage+Event组合的记录您可以在下面使用相关子查询进行尝试
select * from tablename t1
where score in
(select max(score) from tablename t2 where t1.name=t2.name and t1.stage=t2.stage
and t1.eventid=t2.eventid)
您可以在子查询上使用内部联接来获取所需的最大值
select yt.*
from your_table yt
INNER JOIN (
select Name, Stage_ID, Event_ID, Attempt_No, max(score) max_score
from your_table
group by Name, Stage_ID, Event_ID, Attempt_No
) t on yt.name = t.name
and yt.Stage_ID = t.Stage_ID
and yt.Event_ID = t.Event_ID
and yt.Attempt_No = t.Attempt_No
and yt.score = t.max_score join
Why in and not=?如果有两行具有相同的最大分数怎么办?你想选择哪一个?非常感谢,我用了另一个答案,它成功了。感谢您的快速响应和帮助。