如表结构:id,name,age,type,Score数据据:1,李,12,小学生,802,王,13,小学生,783,王,13,中学生,564,王,13,中学生,945,王,13,小学生,356,王,13,中学生,747,王,13,小学生,978,王,13,中学生,64用一条sql语句得出下面的结1,李,12,小学生,807,王,13,小学生,974,王,13,中学生,946,王,13,中学生,74
3 回答
![?](http://img1.sycdn.imooc.com/5458683f00017bab02200220-100-100.jpg)
慕少森
TA贡献2019条经验 获得超9个赞
这个sql有点麻烦了,
简单点的就直接
select id,name,type,score from student where type='小学生' limit 2 union select id,name,type,score from student where type='中学生' limit 2 ;
或者你用存储过程来做循环:查询出所有type然后在循环里动态给 查询语句中的type复制,有几个type就concat几个union上去,
其他直接写sql查询的话有点难,写不出来
![?](http://img1.sycdn.imooc.com/5458477300014deb02200220-100-100.jpg)
临摹微笑
TA贡献1982条经验 获得超2个赞
select id,name,age,type,score from(
select id,name,age,type,score,rank() over(partition by school order by id asc) as num from table aa)
where aa.num<3
这个可以我试过了
添加回答
举报
0/150
提交
取消