mysql 连接查询的详细讲解
来源:互联网 发布:战地4优化补丁 编辑:程序博客网 时间:2024/06/08 06:34
---------建表 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
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
阅读全文
0 0
- mysql 连接查询的详细讲解
- mysql的连接查询
- mysql的连接查询
- Python连接MySQL(融合多篇文章)详细讲解
- Oracle中表的连接方式查询讲解
- 详细讲解MySQL数据库的安全配置
- MySQL权限的详细讲解和示例
- MySql基本查询、连接查询、子查询、正则表达查询讲解
- MySql基本查询、连接查询、子查询、正则表达查询讲解
- Mysql的join(连接查询)
- Mysql 连接查询 Mysql支持的连接查询有哪些
- HQL查询详细语法讲解
- 详细讲解双查询注入
- MySQL的子查询与连接查询
- MySQL的连接查询及子查询
- 【mysql】mysql的查询、子查询及连接查询
- 详细讲解提高数据库查询效率的实用方法
- 详细讲解提高数据库查询效率的实用方法
- 不错的linux下通用的java程序启动脚本
- IDEA远程调试Tomcat热替换
- NULL知识
- adb remount失败的一种解决办法
- golang.org/x/net/websocket 报错
- mysql 连接查询的详细讲解
- 线程总结(一)
- git项目迁移的两种方式
- 可伸缩性架构常用技术——数据库sharding/partition
- Java编程思想之网络编程(一)机器的标识
- 同一台linux服务器搭建两个mysql
- 表单引入validate验证
- 作为一个php开发者,ubuntu(16.04)安装完成后要做的几件事情
- Maven2与Ivy的区别