当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回,本次将向大家介绍如何使用 MySQL 的 JOIN 在两个或多个表中查询数据。
1、INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
方式一:
select * from 表1,表2 where 表1.列=表2.列
方式二:
select * from 表1
inner join 表2 on 表1.列=表2.列
例1:查询学生信息及学生的成绩
select * from students as stu,scores as sc where
stu.studentNo = sc.studentNo
---------------------------------------
select * from students as stu
inner join scores as sc on stu.studentNo = sc.studentNo
例2:查询课程信息及课程的成绩
select * from courses cs,scores sc where
cs.courseNo = sc.courseNo
---------------------------------------
select * from courses as cs
inner join scores as sc on cs.courseNo = sc.courseNo
例3:查询学生信息及学生的课程对应的成绩
select * from students as stu,courses as cs,scores as sc
where
stu.studentNo = sc.studentno
and cs.courseNo = sc.courseNo
---------------------------------------
select * from students as stu
inner join scores as sc on stu.studentNo = sc.studentNo
inner join courses cs on cs.courseNo = sc.courseNo
例4:查询王昭君的成绩,要求显示姓名、课程号、成绩
select stu.name,sc.courseNo,sc.score
fromstudents stu,scores sc
where
stu.studentNo = sc.studentNo
and stu.name = '王昭君'
---------------------------------------
select stu.name,sc.courseNo,sc.score
from
students stu
inner join scores sc on stu.studentNo = sc.studentNo
where
stu.name = '王昭君'
2、LEFT JOIN(左连接):查询的结果为两个表匹配到的数据加左表特有的数据,对于右表中不存在的数据使用null填充
语法:
select * from 表1
left join 表2 on 表1.列=表2.列
例1:查询所有学生的成绩,包括没有成绩的学生
select * from students as stu
left join scores as sc on stu.studentNo = sc.studentNo
例2:查询所有学生的成绩,包括没有成绩的学生,需要显示课程名
select * from students as stu
left join scores as sc on stu.studentNo = sc.studentNo
left join courses cs on cs.courseNo = sc.courseNo
3、RIGHT JOIN(右连接): 与 LEFT JOIN 相反,查询的结果为两个表匹配到的数据加右表特有的数据,对于左表中不存在的数据使用null填充。
语法:
select * from 表1
right join 表2 on 表1.列=表2.列
例1:查询所有课程的成绩,包括没有成绩的课程
select * from scores as sc
right join courses as cs on cs.courseNo = sc.courseNo
例2:查询所有课程的成绩,包括没有成绩的课程,包括学生信息
select * from scores as sc
right join courses as cs on cs.courseNo = sc.courseNo
left join students stu on stu.studentNo = sc.studentNo
数据库使用的表
students表
drop table if exists students;
create table students (
studentNo varchar(10) primary key,
name varchar(10),
sex varchar(1),
hometown varchar(20),
age tinyint(4),
class varchar(10),
card varchar(20)
)
insert into students values
('001', '王昭君', '女', '北京', '20', '1班', '340322199001247654'),
('002', '诸葛亮', '男', '上海', '18', '2班', '340322199002242354'),
('003', '张飞', '男', '南京', '24', '3班', '340322199003247654'),
('004', '白起', '男', '安徽', '22', '4班', '340322199005247654'),
('005', '大乔', '女', '天津', '19', '3班', '340322199004247654'),
('006', '孙尚香', '女', '河北', '18', '1班', '340322199006247654'),
('007', '百里玄策', '男', '山西', '20', '2班', '340322199007247654'),
('008', '小乔', '女', '河南', '15', '3班', null),
('009', '百里守约', '男', '湖南', '21', '1班', ''),
('010', '妲己', '女', '广东', '26', '2班', '340322199607247654'),
('011', '李白', '男', '北京', '30', '4班', '340322199005267754'),
('012', '孙膑', '男', '新疆', '26', '3班', '340322199000297655')
courses表
drop table if exists courses;
create table courses (
courseNo int(10) unsigned primary key auto_increment,
name varchar(10)
);
insert into courses values
('1', '数据库'),
('2', 'qtp'),
('3', 'linux'),
('4', '系统测试'),
('5', '单元测试'),
('6', '测试过程');
scores表
drop table if exists scores;
create table scores (
id int(10) unsigned primary key auto_increment,
courseNo int(10),
studentno varchar(10),
score tinyint(4)
);
insert into scores values
('1', '1', '001', '90'),
('2', '1', '002', '75'),
('3', '2', '002', '98'),
('4', '3', '001', '86'),
('5', '3', '003', '80'),
('6', '4', '004', '79'),
('7', '5', '005', '96'),
('8', '6', '006', '80');