内连接

内连接,也叫等值连接, inner join得出同时存在t1表和t2表的数据集,通俗一点说就是求两个表的交集。

-- join
select * from course c join teacher t ON c.t_id = t.t_id 

-- inner join
select * from course c inner join teacher t on c.t_id = t.t_id 

-- 逗号的连表方式就是内连接
select * from course c ,  teacher t where c.t_id = t.t_id 

外连接(左右连接)

左连接:left [outer] join,左连接从左表取出所有记录,与右表匹配。如果没有匹配,以null值代表右边表的列。outer 可以不写,默认情况下不写outer关键字。

-- left join
select * from course c left join teacher t  on  c.t_id = t.t_id 

-- left outer join
select * from course c left outer join teacher t  on c.t_id = t.t_id 

右连接:right [outer] join,右连接从右表取出所有记录,与左表匹配。如果没有匹配,以null值代表左边表的列。outer 可以不写,默认情况下不写outer关键字。

-- right join
select * from course c right join teacher t on   c.t_id = t.t_id 

-- right outer join
select * from course c right outer join teacher t on   c.t_id = t.t_id 

全连接

两个表的并集,MySQL暂不支持这种语句,不过可以使用union将两个结果集“堆一起”,利用左连接,右连接分两次将数据取出,然后用union将数据合并去重。

-- oracle的全连接
select * from a full join b on a.id = b.id

-- mysql的全连接
-- mysql中没有full join,mysql可以使用union实现全连接;
select * from a left join b on a.id = b.id
union
select * from a right join b on a.id = b.id