高级查询涉及到的数据请查看:
Mysql系列课程–第三章 建表 插数据
一.表连接
1.内连接
1.1等值内连接
/李文辉是哪个班的/
select s.s_name,c.c_name from student s inner join class c on s.c_no = c.c_no
where s.s_name='李文辉';(第一种写法)
select s.s_name,c.c_name from student s,class c where s.c_no = c.c_no
and s.s_name='李文辉';(第二种写法)
/李文辉报考了哪些门课/
select s_name,sb_name from student s,result r ,subject sb where s.s_no=r.s_no and
r.sb_no = sb.sb_no and s.s_name='李文辉';(第一种写法)
select s_name,sb_name from (student s inner join result r on s.s_no = r.s_no)inner
join subject sb on r.sb_no = sb.sb_no where s.s_name='李文辉';(第二种写法)
/每一门课有多少人报考/
select sb_name,count(*) from student s,result r ,subject sb where s.s_no=r.s_no
and r.sb_no = sb.sb_no group by sb_name;
1.2不等值内连接
select s.s_name,c_name from student s,class c
where not s.c_no > c.c_no and not s.c_no < c.c_no and s.s_name='李文辉';
2.外连接
2.1左外连接
/查看各门课的成绩/
select sb_name,score from subject s left join result r on s.sb_no = r.sb_no;
/各老师的代课平均分/
select t_name,avg(score) from teacher t left join subject sb on t.t_no
= sb.t_no left join result r on r.sb_no = sb.sb_no group by t_name
2.2 右外连接
/学生的分班情况/
select s_name,c_name from class c right join student s on s.c_no = c.c_no;
二.子查询
1.单行子查询
/李文辉在哪个班/
select c.c_name from class c where c_no = (select c_no from student where s_name
= '李文辉');
/极客巅峰有哪些学生/
select s_name from student where c_no = (select c_no from class where c_name='极客巅峰');
/查询出年龄最大的学生/
select s_name from student where s_age = (select max(s_age) from student);
2.多行子查询
/查询大于5人的班级学生信息/
select * from student where c_no in (select c_no from class where c_num>5);
/查询比女生年龄 都 大的男生/
select * from student where s_age > ALL (select s_age from student where s_sex='女')
and s_sex = '男';
/查询比女生年龄大的男生/
select * from student where s_age > ANY (select s_age from student where s_sex='女')
and s_sex = '男';
2.多列子查询
/查询年龄大于25的男生/(不要在意这些细节,知识点重要)
select * from student where (s_sex,s_age) in (select s_sex,s_age from student
where s_sex='男' and s_age>25);
3.嵌套子查询
/嵌套在where后面/
select * from student where c_no = (select c_no from class where c_name='极客巅峰');
/嵌套在from后面/(不要在意这些细节,知识点重要)
select * from (select s_name from student) s;
/嵌套在select后面/(不要在意这些细节,知识点重要)
select (select s_name from student where s_name='李文辉') from student where s_name='李文辉';
/极客巅峰JAVA课程谁考了第一/
select s_name,sb_name,score from student s,class c,subject sb,result r where s.c_no
=c.c_no and sb.sb_no = r.sb_no and r.s_no=s.s_no and c.c_name='极客巅峰' and sb.sb_name
='java' and r.score=(select max(score) from result r,subject sb where r.sb_no = sb.sb_no
and sb.sb_name='java' );
/每门学科的状元(第一名)/
select s_name,sb_name,score from student s,result r,subject sb where s.s_no = r.s_no and
r.sb_no = sb.sb_no and (sb_name,score) in (select sb_name,max(score) m from result r ,
subject sb where r.sb_no = sb.sb_no group by sb.sb_name);(第一种写法)
select s_name,sb_name,score from student s,result r,subject sb,(select sb_name sn,
max(score) ms from result r ,subject sb where r.sb_no = sb.sb_no group by sb.sb_name) m
where s.s_no = r.s_no and r.sb_no = sb.sb_no and sb.sb_name = m.sn and m.ms = r.score;
(第二种写法)
/各班年龄最大的学生/
select s_name,c_name,s_age from student s,class c where s.c_no = c.c_no and
(c_name,s_age) in (select c_name,max(s_age) from student s,class c
where s.c_no = c.c_no group by c.c_name)
三. case when用法
/男女性别互换/
select s_name,case s_sex when '男' then '女' when '女' then '男' end
from student;
/学生各门课的考试等级/
select s_name,sb_name,score,case when score>90 then 'A' when score>80 then 'B'
when score>70 then 'C' else 'D' end '等级' from result r,student s,subject sb
where r.s_no = s.s_no and r.sb_no = sb.sb_no;
详细课程查询:
Mysql系列课程–第一章 Sql分类 常用命令
Mysql系列课程–第二章 约束 自增主键
Mysql系列课程–第三章 建表 插数据
Mysql系列课程–第四章 备份 普通查询
Mysql系列课程–第五章 高级查询 表连接 子查询 case when
Mysql系列课程–第六章 索引和视图
Mysql系列课程–第七章 触发器和存储过程
Mysql系列课程–第八章 sql精选35道题