首页 > 代码库 > mysql left join,right join,inner join简单实例

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;

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

uidsidsidsalary
11111
22NULLNULL


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

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

uidsidsidsalary
11111
NULLNULL333


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

相当于上面两表求交集

uidsidsidsalary
11111

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