sql查询

张序员 2017-11-28 03:19:03

查出所有 每个sid含有最大score的记录
...全文
125 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2017-11-28
  • 打赏
  • 举报
回复
select t.id,a.sid,a.score from stu_score t,(select sid,max(score) score from stu_score group by sid) a where t.sid =a.sid and t.score=a.score ; 这个是通用的
张序员 2017-11-28
  • 打赏
  • 举报
回复
引用 1 楼 qq646748739 的回复:
试试这个:

select id,sid,score
  from(select id,sid,score,row_number()over(partition by sid order by score desc) rn
         from 表名称
      ) 
  where rn = 1
  
还有,抱歉,我这是mysql 因为分类中没有,就选了oracle。。。所以函数可能没有,尽量别用函数
张序员 2017-11-28
  • 打赏
  • 举报
回复
引用 1 楼 qq646748739 的回复:
试试这个:

select id,sid,score
  from(select id,sid,score,row_number()over(partition by sid order by score desc) rn
         from 表名称
      ) 
  where rn = 1
  
CREATE TABLE `stu_score` ( `id` int(11) NOT NULL AUTO_INCREMENT, `sid` int(11) DEFAULT NULL, `score` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; 这是表信息,试一下,如得到结果马上结贴
碧水幽幽泉 2017-11-28
  • 打赏
  • 举报
回复
没问题的话,此楼结贴!
碧水幽幽泉 2017-11-28
  • 打赏
  • 举报
回复
试试这个:

select id,sid,score
from(select id,sid,score,row_number()over(partition by sid order by score desc) rn
from 表名称
)
where rn = 1

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧