SQL的几种连接查询方式(内连接、外连接、全连接、联合查询) | 您所在的位置:网站首页 › sql数据库联合查询 › SQL的几种连接查询方式(内连接、外连接、全连接、联合查询) |
准备表 drop table if exists test_a; CREATE TABLE `test_a` ( `id` varchar(10) NOT NULL, `username` varchar(10) NOT NULL, `password` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; drop table if exists test_a_description; CREATE TABLE `test_a_description` ( `id` varchar(10) NOT NULL, `age` varchar(10) , `address` varchar(50) , `parent_id` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;准备数据 insert into test_a values('1','小明','11'); insert into test_a values('2','宁宁','22'); insert into test_a values('3','敏敏','33'); insert into test_a values('6','生生','66'); insert into test_a_description values('1','10','aaa','1'); insert into test_a_description values('2','20','bbb','2'); insert into test_a_description values('3','30','ccc','3'); insert into test_a_description values('4','40','ddd','4');一、内连接(inner join)
查出的是两张表的交集,两张表都有的才查出来 SQL: select * from 表A inner join 表B on 判断条件; select * from 表A, 表B where 判断条件; select a.*, ad.* from test_a as a inner join test_a_description as ad on a.id=ad.parent_id; select a.*, ad.* from test_a as a, test_a_description as ad where a.id=ad.parent_id;结果: 二、外连接 1、左外连接(left join)
以左表为主表(查询全部), 右表为辅表(没有的显示null) SQL: select * from 表A left join 表B on 判断条件; select a.*, ad.* from test_a as a left join test_a_description as ad on a.id=ad.parent_id;结果: 以右表为主表(查询全部), 左表为辅表(没有的显示null) SQL: select * from 表A right join 表B on 判断条件; select a.*, ad.* from test_a as a right join test_a_description as ad on a.id=ad.parent_id;
结果:
两个表的所有数据都展示出来 SQL: select * from 表A full join 表B on 判断条件; select a.*, ad.* from test_a as a full join test_a_description as ad on a.id=ad.parent_id;
结果: MySQL 不识别 FULL join,所以可以通过 union 来实现 SQL: select a.*, ad.* from test_a as a left join test_a_description as ad on a.id=ad.parent_id union select a.*, ad.* from test_a as a right join test_a_description as ad on a.id=ad.parent_id;
结果: 五 交差集 还需要注册的是我们还有一个是“交差集” cross join, 这种Join没有办法用文式图表示,因为其就是把表A和表B的数据进行一个N*M的组合,即笛卡尔积。表达式如下: SELECT * FROM TableA CROSS JOIN TableB 这个笛卡尔乘积会产生 4 x 4 = 16 条记录,一般来说,我们很少用到这个语法。但是我们得小心,如果不是使用嵌套的select语句,一般系统都会产生笛卡尔乘积然再做过滤。这是对于性能来说是非常危险的,尤其是表很大的时候。 六、区别分析外连接:外连不但返回符合连接和查询条件的数据行,还返回不符合条件的一些行。外连接分三类:左外连接(LEFT OUTER JOIN)、右外连接(RIGHT OUTER JOIN)和全外连接(FULL OUTER JOIN) 三者的共同点是都返回符合连接条件和查询条件(即:内连接)的数据行。不同点如下: 左外连接:还返回左表中不符合连接条件单符合查询条件的数据行。 右外连接:还返回右表中不符合连接条件单符合查询条件的数据行。 全外连接:还返回左表中不符合连接条件单符合查询条件的数据行,并且还返回右表中不符合连接条件单符合查询条件的数据行。全外连接实际是上左外连接和右外连接的数学合集(去掉重复),即“全外 = 左外 UNION 右外”。 说明:左表就是在“(LEFT OUTER JOIN)”关键字左边的表。右表当然就是右边的了。在三种类型的外连接中,OUTER 关键字是可省略的。 七 、SQL查询的基本原理单表查询:根据WHERE条件过滤表中的记录,形成中间表(这个中间表对用户是不可见的);然后根据SELECT的选择列选择相应的列进行返回最终结果。 两表连接查询:对两表求积(笛卡尔积)并用ON条件和连接连接类型进行过滤形成中间表;然后根据WHERE条件过滤中间表的记录,并根据SELECT指定的列返回查询结果。 多表连接查询:先对第一个和第二个表按照两表连接做查询,然后用查询结果和第三个表做连接查询,以此类推,直到所有的表都连接上为止,最终形成一个中间的结果表,然后根据WHERE条件过滤中间表的记录,并根据SELECT指定的列返回查询结果。 理解SQL查询的过程是进行SQL优化的理论依据。 |
CopyRight 2018-2019 实验室设备网 版权所有 |