热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

mysql多表连接查询inner/left/right/full/crossjoin

---------建表table1,tabel2---------createtabletable1(idint,namevarchar(20))createtabletable2(i
---------建表 table1,tabel2---------
create table table1(id int, name varchar(20))
create table table2(id int, age int)

insert into table1 select 1,'zhangsan'
insert into table1 select 2,'lisi'
insert into table1 select 3,'wangwu'

insert into table2 select 1,20
insert into table2 select 2,30
insert into table2 select 4,25

-----------------------如表----------------------
table1                     |         table2
---------------------------------------------------
id    name               |         id    age
1     zhangsan         |        1     20
2     lisi                    |        2     30
3     wangwu            |        4     25
(1) 内连接 (inner join /  join): 两表都满足的组合
-----------------------sql语句---------------------------
select * from table1 join table2 on table1.id=table2.id
-----------------------结果-------------------------------
 id         name               id            age
 1          zhangsan        1             20
 2          lisi                   2             30
(2) 全连接 (full outer join /   full join ):两表相同的组合在一起,A表有,B表没有的数据(显示为null),
同样B表有的A表没有显示为null.
-----------------------sql语句---------------------------
select * from table1 full join table2 on table1.id=table2.id
-----------------------结果-------------------------------
 id         name               id            age
 1          zhangsan        1             20
 2          lisi                   2             30
 3           wangwu          null         null 
 null        null                 4            25
(3) 左连接 (left join   /  left outer join):A表left join B表 左连,以A表为基础,A表的全部数据,B表有的组合,没有的为null
-----------------------sql语句---------------------------
select * from table1 left join table2 on table1.id=table2.id
-----------------------结果-------------------------------
 id         name               id            age
 1          zhangsan        1             20
 2          lisi                   2             30
 3           wangwu          null         null 
 
(4) 右连接 (right join  / right  outer join):A表right join B表 右连,以B表为基础,B表的全部数据,A表有的组合,没有的为null
-----------------------sql语句---------------------------
select * from table1 right join table2 on table1.id=table2.id
-----------------------结果-------------------------------
 id         name               id            age
 1          zhangsan        1             20
 2          lisi                   2             30
 null       null                  4            25 
(5) 交叉连接 (cross join):没有where子句的交叉联接将产生联接所涉及的表的笛卡尔积,第一个表的行数
乘以第二个表的行数等于笛卡尔积结果集的大小(table1和table2交叉连接产生3*3=9条记录)
-----------------------sql语句---------------------------
select * from table1 cross join table2 与 select * from table1, table2相同
-----------------------结果-------------------------------
 id         name               id            age
 1          zhangsan        1             20
 2          lisi                   1             20
 3           wangwu          1             20
 1          zhangsan        2             30
 2          lisi                   2             30
 3           wangwu          2             30
 1          zhangsan        4             25
 2          lisi                   4             25
 3           wangwu          4             25
推荐阅读
author-avatar
飞天6585_439
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有