内连接 : 将两张表相同意义字段连接起来
select * from A,B where A.A_ID = B.A_ID; 条件 A表中A_ID与 B表中 A_ID 相等匹配
* 返回结果一定是两个表都存在信息 , 最有意义的信息,如果第一张表记录在第二张表找不到匹配信息,不显示,第二张表记录在第一张表无匹配信息,不显示
第一张表10条数据
第二张表20条数据
内连接 结果 <= 10条
语法:select * from a inner join b on A.A_ID = B.A_ID;
简化:select * from a,b where A.A_ID = B.A_ID;
外连接:左外连接、右外连接、全外连接
左外连接 :用第一张表每条记录去匹配第二张表对应记录,无论是否找到匹配信息,都显示第一张表匹配结果
例如:每个水果价格 ? 没有价格水果也要显示
select * from a left outer join b on A.A_ID = B.A_ID ;
第一张表10条数据
第二张表20条数据
左外连接 --- 10条
右外连接:从第二张表找第一张表匹配记录,无论是否找到,第二张表所有记录都显示
select * from a right outer join b on A.A_ID = B.A_ID ;
第一张表10条数据
第二张表20条数据
右外连接 --- 20条
全外连接:左外连接与右外连接 结果和 ---- 排除重复数据
select * from a full outer join b on A.A_ID = B.A_ID ; ----- MySQL 不支持
使用union关键字实现全外连接效果
select * from A left outer join B on A.A_ID = B.A_ID
union
select * from A right outer join B on A.A_ID = B.A_ID;
------------------------------------------------------------------------------------
关联子查询:将第一个查询结果 ,作为第二个查询条件
查询student表中年龄最大学员的信息
select * from student where age = (select max(age) from student);
等价于
select max(age) from student; ----- 25
select * from student where age = 25; ----- 学生信息
IN/EXISTS 当前查询记录在子查询结果中存在
查询所有成绩小于60分的同学名称
查询studentcource表成绩小于60 所有记录
select student_id from studentcource where score < 60; --- 小于60分学生学号 2,8
再根据id去查询学生表,得知学生姓名
select * from student where id in(2,8);
select * from student where id in(select student_id from studentcource where score < 60);
exists实现上面in 语句效果
select name from student where exists (select * from studentcource where score < 60 and student.id = studentcource.student_id);
select * from studentcource,student where score < 60 and student.id = studentcource.student_id;
select name from student where exists (select * from studentcource where score < 60 and student.id = studentcource.student_id);
查询获得最高分的学生学号
select max(score) from studentcource; 最高学分
select student_id from studentcource where score = (select max(score) from studentcource);
* 自我比较
select student_id from studentcource where score >=all(select score from studentcource);
查询编号2课程比编号1课程成绩高所有学号
select score from studentcource where cource_id = 2 and score > any(select score from studentcource where cource_id = 1);
select score from studentcource where cource_id = 2; 课程2所有成绩
select score from studentcource where cource_id = 1; 课程1所有成绩
使用union将两个查询结果合并,union 排重重复数据 union all 不会排重重复数据
* 合并时列名必须一致
select t1.student_id,t1.score 语文,t2.score 数学 from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name=‘语文‘) t1,(select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name=‘数学‘) t2 where
t1.student_id = t2.student_id and t1.score > t2.score;
查询平均成绩大于70分的同学的学号和平均成绩
* 按人取平均成绩 ------ 分组
select student_id,avg(score) from studentcource group by student_id having avg(score)>70;
* 打印学生姓名
select student.name,t.avgscore from student,(select student_id,avg(score) avgscore from studentcource group by student_id having avg(score)>70) t where student.id = t.student_id;
查询所有同学的学号、姓名、选课数、总成绩
*学生信息:select * from student;
*选课数、总成绩 select student_id,count(*),sum(score) from studentcource group by student_id;
select student.id 学号,student.name 姓名,t.courcenum 选课数, t.sumscore 总成绩 from student,(select student_id,count(*) courcenum,sum(score) sumscore from studentcource group by student_id) t where student.id = t.student_id;
查询没学过关羽老师课的同学的学号、姓名
* 关羽老师教什么课 select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name=‘关羽‘;
* 选过关羽老师课 select distinct student_id from studentcource where cource_id in (select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name=‘关羽‘);
select id,name from student where id not in (select distinct student_id from studentcource where cource_id in (select cource.id from teacher,cource where teacher.id = cource.teacher_id and teacher.name=‘关羽‘));
查询学过语文并且也学过数学课程的同学的学号、姓名
* 语文和数据 课程编号 select id from cource where name=‘语文‘ or name=‘数学‘;
* 学过语文的学生 select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name=‘语文‘;
* 学过数学的学生 select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name=‘数学‘;
select t1.student_id from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name=‘语文‘) t1, (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name=‘数学‘) t2 where t1.student_id = t2.student_id
;
select student.id,student.name from student,(select t1.student_id from (select * from cource,studentcource where cource.id = studentcource.cource_id and cource.name=‘语文‘) t1, (select * from cource,studentcource where cource.id = studentcource.cource_id and
cource.name=‘数学‘) t2 where t1.student_id = t2.student_id) t where student.id = t.student_id;