student表,score表,
student的id字段和score的studentid字段关联。
student表中有1,2,而score表中有2,3。student表中有score表中没有的1,score表中有student表中没有的3.有一个交集是2。
drop table student;
create table student(
id int not null primary key,
name varchar(32)
);
drop table score;
create table score(
studentid int,
score int
);
insert into student(id,name) values (1,'wang');
insert into student(id,name) values (2,'liu');
insert into score(studentid,score) values(2,20);
insert into score(studentid,score) values(3,30);
内连接查询
select student.*,score.* from student,score where student.id = score.studentid;
查询结果:
左外连接
主表(左表)student中的数据逐条匹配表score中的数据
1、匹配,返回到结果集
2、无匹配,NULL值返回到结果集
select student.*,score.* from student left join score on student.id = score.studentid;
select score.*,student.* from score left join student on student.id = score.studentid;
右外连接
右表逐条去匹配记录;否则NULL填充
select student.*,score.* from student right join score on student.id = score.studentid;
select score.*,student.* from score right join student on student.id = score.studentid;