mysql left join,right join,inner join简单实例

-- 准备工作

drop table if exists Emp;
create table if not exists Emp
(
    uid int primary key,
    sid int
);
insert into Emp values(1,1);
insert into Emp values(2,2);

drop table if exists Sal;
create table if not exists Sal
(
    sid int primary key,
    salary float
);
insert into Sal values(1,11);
insert into Sal values(3,33);

-- 查看结果
select * from Emp left  join Sal on Emp.sid = Sal.sid;

左表为主(左表内容至少全部出现一次)

uid sid sid salary
1 1 1 11
2 2 NULL NULL


select * from Emp right join Sal on Emp.sid = Sal.sid;

右表为主(右表内容至少全部出现一次)

uid sid sid salary
1 1 1 11
NULL NULL 3 33


select * from Emp inner join Sal on Emp.sid = Sal.sid;

相当于上面两表求交集

uid sid sid salary
1 1 1 11

mysql left join,right join,inner join简单实例

上一篇:【VB.NET 机房收费系统】——对SqlHelper别说“不”


下一篇:mysql查询优化方案