Oracre和Mysql的分页,以及删除重复记录

t_student(id,stuname,sex,age,areaid,classid)


t_area(id,areaname)


t_class(id,className)




--查询出学生名以及学生所在班级名
select s.stuName,c.className from t_student s left join t_class c on s.classId = c.id;




--查询年龄在18-30之间的女生姓名以及所在的班级名
select s.stuName,c.className from t_student s 
left join t_class c on s.classId = c.id
where s.sex=0 and s.age>=18 and s.age<=30; 


--查询年龄在18-30之间的山东女生姓名以及所在的班级名并且按照年龄进行升序排列
select s.stuName,s.age,c.className from t_student s 
left join t_class c on s.classId = c.id
left join t_area a on s.areaId = a.id
where s.sex=0 and s.age>=18 and s.age<=30 and a.areaName='山东'
order by s.age asc;




--查询年龄在18-30之间的姓张的女生姓名以及所在的班级名并且按照年龄进行升序排列
select s.stuName,c.classname,s.age from t_student s 
left join t_class c on s.classid=c.id
where s.age>=18 and s.age<=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 c.className,s.sex,count(*),avg(age) from t_student s 
left join t_class c on s.classId = c.id
group by c.className,s.sex;


--查询各班中各个地区男女生人数
select c.className,a.areaName,s.sex,count(*) from t_student s 
left join t_class c on s.classId = c.id
left join t_area a on s.areaId = a.id
group by s.sex,c.className,a.areaName;


--删除学生表中的重复记录[姓名,年龄,地区相同的]
delete from t_student where rowid not in 
(
select min(rowid) from t_student s 
left join t_area a on a.id = s.areaId
group by s.name,s.age,a.areaName

)


--删除山西姓李的未成年男生
delete from t_student where id in(
select s.id from t_student s 
left join t_area a on s.areaId=a.id
where s.age<18 and a.areaName = '山西' and s.stuName like '李%' and s.sex = 1;
)


--oracle的分页,查询未成年的男生并按照年龄进行倒叙排列,并进行分页。
【每页有13条数据,查询第33页的数据】


select * from
(
select t.* ,rownum rn from
(
select * from t_student 
where age < 18 and sex=1 
order by age desc
) t
where rownum <= 429
)
where rn >= 417;


--mysql的分页,查询未成年的男生并按照年龄进行倒叙排列,并进行分页。
【每页有13条数据,查询第33页的数据】


select * from 
(
select * from t_student 
where age < 18 and sex = 1
order by age desc 
)
limit 416,13;

猜你喜欢

转载自blog.csdn.net/weixin_40836026/article/details/80437427
今日推荐