小梦用极简单的方式带小伙伴们过一遍内连接与外连接。
?表1 classa?????????????
?????????
表2 classb
🚀1. 内连接 inner join (join 默认就是内连接)
表1与表2的交集,用上面两个表演示一下
select
classa.id as aid,classb.id as bid
from
classa
inner join classb on classa.id = classb.id;
?查询的结果是classa与classb的交集
🚀?2. 左外连接 left join
结果集保留左表的所有行,但右表只包含与左表匹配的行。右表相应的空行为NULL值。
select
classa.id as aid,classb.id as bid
from
classa
left join classb on classa.id = classb.id;
?🚀?3. 右外连接 right join
结果集保留右表的所有行,但左表只包含与右表匹配的行。左表相应的空行为NULL值。
select
classa.id as aid,classb.id as bid
from
classa
right join classb on classa.id = classb.id;
?🚀4. 全外连接 full join
会把两个表所有的行都显示在结果表中。
select
classa.id as aid,classb.id as bid
from
classa
full join classb on classa.id = classb.id;
小伙伴们注意啦!!!
MySQL不支持full join!!!MySQL不支持full join!!!MySQL不支持full join!!!
重要的事情要说三遍!!!那怎么实现和full join一样的效果呢?那就要通过使用union来实现,具体实现SQL语句如下
select
classa.id as aid,classb.id as bid
from
classa left join classb on classa.id = classb.id
union
select
classa.id as aid,classb.id as bid
from
classa right join classb on classa.id = classb.id;