美文网首页数据库
MySQL 内联(John on)外联(left John on

MySQL 内联(John on)外联(left John on

作者: 孙掌门 | 来源:发表于2018-03-20 09:32 被阅读7次

数据库的内连接,外链接,和交叉连接

首先我们新建两个表,分别为员工表和部门表

1.内联(John on或inner join on)

select * from dept as d JOIN emp as e where d.id = e.deptid;

执行上面的spl语句后结果为,

可见内联搜索出两个表同时存在的结果。

2.外联(左外联left join或left outer on和右外联right on 或 right outer on)

select *from dept as d left JOIN emp as e on d.id = e.deptid;

执行之后结果为

可见,左外联,搜索结果为,left join左边的表的字段对应的结果,如果左边的表对应的字段右边的表没有,那么以null填充。

rom dept as d right JOIN emp as e on d.id = e.deptid;

那么右外联的搜索结果怎样呢?

会搜索出右表所有的字段,如果左表没有,会补null。

3.交叉连接(cross join on)

第一种,不带条件

select *from dept d cross JOIN emp e;

可见结果生成的是一个笛卡尔积

如果带条件,

select *from dept d cross JOIN emp e where d.id = e.deptid;

可见,结果和内联是一样的。

相关文章

网友评论

    本文标题:MySQL 内联(John on)外联(left John on

    本文链接:https://www.haomeiwen.com/subject/pbqyqftx.html