mysql多表连接查询inner/left/right/full/crossjoin
作者:飞天6585_439 | 来源:互联网 | 2024-09-27 11:53
---------建表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
推荐阅读
-
MySQL的SQL语句写法,除了那些基本的之外,还有一些也算比较常用的,这里记录下来,以便以后查找。好记性不如烂笔头& ...
[详细]
蜡笔小新 2024-09-25 13:04:19
-
一介绍MySQL数据操作:DML在MySQL管理软件中,可以通过SQL语句中的DML语言来实现数据的操作,包括使用INSERT实现数据的插 ...
[详细]
蜡笔小新 2024-09-26 13:18:53
-
-
本篇文章给大家主要讲的是关于如何实现PerconaMysqlGalera多读写集群的部署的内容,感兴趣的话就一起来看看这篇文章吧,相信看完如何实现PerconaMysq ...
[详细]
蜡笔小新 2024-09-27 14:40:41
-
你到底是如何存储XML的?以下对我有用:createtablet(c1varchar2(1000));insertintotval ...
[详细]
蜡笔小新 2024-09-26 11:46:26
-
MySQL重大Bug!自增主键竟然不是连续递增? ...
[详细]
蜡笔小新 2024-09-24 17:24:14
-
第一部分:TSqlTop有两种用法1,限制查询结果集返回的行数或总行数的百分比。当将TOP与ORDERBY子句结合使用时,结果集限制为前N个已排序行;否则,以未定义的顺序返回前N个 ...
[详细]
蜡笔小新 2024-09-27 19:54:10
-
接上文http:blog.itpub.net29254281viewspace-1318239领导让开发同学鼓捣一个可配置化的后台.又回到了原来的问题如果要灵活,很多参数要 ...
[详细]
蜡笔小新 2024-09-27 19:50:41
-
环境phpstudyphp服务端代码security数据库中的users表中的username,password字段用户名adminJSON服务端代码大家实际测试中注 ...
[详细]
蜡笔小新 2024-09-27 19:45:58
-
程序死锁的问题,很难调试,看进程堆栈,看各个线程与锁的情况,对照代码进行排查。数据库死锁的问题,更难ÿ ...
[详细]
蜡笔小新 2024-09-27 11:09:15
-
代码如下:(把数据以表格形式输出本人小白哪位大侠能把完整的代码写出来呀最好是可以添加、删除和显示表格的)<%@pagecontentTypetexthtmlpageEn ...
[详细]
蜡笔小新 2024-09-27 09:04:05
-
篇首语:本文由编程笔记#小编为大家整理,主要介绍了图书管理系统(JSP+JDBC+Servlet)实现-01:流程分析和数据库建表阶段相关的知识,希望对你有一定的参考价值。 ...
[详细]
蜡笔小新 2024-09-27 05:56:04
-
本文目录一览:1、MySQL创建数据库的命令、创建表的命令、插入语句的命令 ...
[详细]
蜡笔小新 2024-09-26 18:47:01
-
2019独角兽企业重金招聘Python工程师标准ORACLE常用SQL技巧1.SELECT子句中避免使用“*”当你想在SELECT子句中列出所有的COLUMN时,使用动态SQ ...
[详细]
蜡笔小新 2024-09-26 16:53:56
-
安全等于运算符()这个操作符和操作符执行相同的比较操作,不过可以用来判断NULL值。在两个操作数均为NULL时,其返回值为1而不为NULL;而当一个操作数为NULL时,其返回 ...
[详细]
蜡笔小新 2024-09-25 18:47:30
-
子元素过滤器在页面设计过程中需要突出某些行时,可以通过基本过滤选择器中的:eq()来实现表单中行的凸显,但不能同时让多个表具有相同的效果。在jQuer ...
[详细]
蜡笔小新 2024-09-27 04:00:36
-
飞天6585_439
这个家伙很懒,什么也没留下!