数据库教程:oracle分页、mysql分页、删除重复记录及简单的sql的查询教程

新建三张表:t_student(id,stuname,sex,age,areaid,classid) t_area(id,areaname) t_class(id,classname) –查询出学

新建三张表:t_student(id,stuname,sex,age,areaid,classid)

t_area(id,areaname)

t_class(id,classname)

–查询出学生名以及学生所在班级名select s.stuname,s.sex,s.age,c.classname from t_student s left join t_class c on s.classid=c.id;–查询年龄在18-30之间的女生姓名以及所在的班级名select s.stuname,s.sex,s.age,c.classname from t_student s left join t_class c on s.classid=c.id where s.age between 18 and 30 and s.sex=0;–查询年龄在18-30之间的山东女生姓名以及所在的班级名并且按照年龄进行升序排列select s.stuname,s.sex,s.age,c.classname from t_student s left join t_area a on s.areaid=a.id left join t_class c on s.areaid=c.id where s.age between 18 and 30 and s.sex=0 order by s.age asc;–查询年龄在18-30之间的姓张的女生姓名以及所在的班级名并且按照年龄进行升序排列select s.stuname,c.classname,s.age,s.sex from t_student s inner join t_class c on s.classid=c.id where s.age between 18 and 30 and s.sex=0 and s.stuname like '张%' order by s.age asc;–查询男女生各自的人数以及平均年龄select sex,count(*),avg(age) from t_student group by sex;–查询各班男女生的人数以及平均年龄select s.sex,count(*),avg(s.age),c.classname from t_student s left join t_class c on s.classid=c.id group by s.sex;–查询各班中各个地区男女生人数select count(*),s.sex,a.areaname,c.classname from t_student s left join t_area a on s.areaid=a.id left join t_class c on s.classid=c.id group by s.sex,a.areaname,c.classname;–删除学生表中的重复记录[姓名,年龄,地区相同的]delete from t_student s left join t_area a on s.areaid=a.id where s.id not in(select max(s.id),a.areaname,s.stuname,s.age from t_student left join t_area a group by a.areaname,s.stuname,s.age);

———–delete from t_student whereid not in(select t.* from (select max(s.id) from t_student s group by s.age,s.stuname,s.areaid) t )

–删除山西姓李的未成年男生delete from t_student where id in(select id from t_area where areaname='山西') and stuname like'李%' and age>18 and sex=1

–oracle的分页,查询未成年的男生并按照年龄进行倒叙排列,并进行分页。【每页有13条数据,查询第33页的数据】select * from (select s.*,rownum rn from(select stuname,sex,age from t_student where sex = 1 and age < 18 group by age desc) swhere rownum <= 442)where rn >= 430;

–mysql的分页,查询未成年的男生并按照年龄进行倒叙排列,并进行分页。【每页有13条数据,查询第33页的数据】select s.* from(select stuname,sex,age from t_student where sex = 1 and age < 18 group by age desc ) slimit 429,13

需要了解更多数据库技术:oracle分页、mysql分页、删除重复记录及简单的sql的查询教程,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/799611.html

(0)
上一篇 2021年9月12日
下一篇 2021年9月12日

精彩推荐