1、连接查询
需要从多个表中查询
1.1 等值连接
会把多个表生成一个临时的表,再使用where条件过滤
表之间关联的时候得使用一个同样含义的字段来进行关联,比如说学号与学号
例1:查询学生信息及学生的成绩
(用这个方式生成的时候不会管理你的学号,比如上述会对应学号001的同学直接生成8条成绩记录,学号002的同学页生成8条,如此循环知道最后一个同学)
select * from student2,scores
过滤掉错误信息,用2个表学号是否相等来判断
select * from student2,scores
where student2.id=scores.id
用表名.列名表示太麻烦的话,可以起别名 stu sc
结果也如上图
select * from student2 as stu,scores as sc
where stu.id=sc.id
例2:只想要学生表的姓名和成绩表的成绩
select stu.name,sc.score from student2 as stu,scores as sc
where stu.id=sc.id
1.2 内连接
在连接的过程中,先进行条件的判断,条件符合才会连接生成结果
(上一个会生成临时表,这一个不会)
先查一张表,再去连接另一张表
select * from student2
inner join scores
过滤掉
使用join on条件来过滤
select * from student2 as stu
inner join scores as sc
on stu.id=sc.id
1.3 多表连接
例3:查询课程信息及课程成绩
select * from scores as sc,courses as cs
where sc.courseNo=cs.courseNo
使用内连接
select * from scores as sc
inner join courses as cs
on sc.courseNo=cs.courseNo
查询多张表应该两两之间进行关联
需要用到and
例4:查询学生信息及学生的课程对应的成绩
select * from student2 as stu,scores as sc,courses as cs
where stu.id=sc.courseNo and sc.courseNo=cs