LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
注释:在某些数据库中, LEFT JOIN 称为 LEFT OUTER JOIN。
RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行。
SELECT column_name(s) FROM table_name1 RIGHT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
注释:在某些数据库中, RIGHT JOIN 称为 RIGHT OUTER JOIN。
只要其中某个表存在匹配,FULL JOIN 关键字就会返回行。
SELECT column_name(s) FROM table_name1 FULL JOIN table_name2 ON table_name1.column_name=table_name2.column_name
注释:在某些数据库中, FULL JOIN 称为 FULL OUTER JOIN。
很苦恼 MySQL不支持全关联,只能实现左右关联,通过观察左右关联的结果数据发现,我们可以根据左右关联的结果实现 全关联:
思路很简单:
左关联结果表:
tempa
右关联结果表:
tempb
全关联结果表:
select * from (
select * from tempa
union
select * from tempb) c;
用例验证:
1.建表语句(基础表)
drop table if exists data_stock1;
create table data_stock (account varchar(10),amount1 int,init_date varchar(20));
INSERT INTO `data_stock1` VALUES ('2002', 210, '20170101');
INSERT INTO `data_stock1` VALUES ('2001', 70, '20170101');
INSERT INTO `data_stock1` VALUES ('2001', 200, '20170102');
INSERT INTO `data_stock1` VALUES ('2001', 30, '20170103');
INSERT INTO `data_stock1` VALUES ('2002', 10, '20170102');
drop table if exists data_stock1;
create table data_stock (account varchar(10),amount2 int,init_date varchar(20));
INSERT INTO `data_stock2` VALUES ('2003', 70, '20170102');
INSERT INTO `data_stock2` VALUES ('2001', 200, '20170101');
INSERT INTO `data_stock2` VALUES ('2001', 30, '20170102');
INSERT INTO `data_stock2` VALUES ('2002', 10, '20170102');
INSERT INTO `data_stock2` VALUES ('2003', 30, '20170101');
2.全联接生成实验
SELECT * from data_stock1 ORDER BY init_date;
SELECT * from data_stock2 ORDER BY init_date;
-- 左联 left JOIN 结果存到临时表
Drop table if EXISTS tempA;
create TABLE tempA(account VARCHAR(10),amount1 int,init_date VARCHAR(20),account1 VARCHAR(10),amount2 int,init_date1 VARCHAR(10)) AS
-- 插入的数据
select a.*,b.account as account1,amount2,b.init_date as init_date1 from data_stock1 a LEFT JOIN data_stock2 b
on a.account= b.account and a.init_date=b.init_date;
-- 右联 right JOIN 存到临时表
Drop table if EXISTS tempB;
create TABLE tempB(account VARCHAR(10),amount1 int,init_date VARCHAR(20),account1 VARCHAR(10),amount2 int,init_date1 VARCHAR(10)) AS
-- 插入的数据
select a.*,b.account as account1,amount2,b.init_date as init_date1 from data_stock1 a RIGHT JOIN data_stock2 b
on a.account= b.account and a.init_date=b.init_date;
-- 全联 full outer JOIN
Drop table if EXISTS tempc;
create table tempc(account VARCHAR(10),amount1 int,init_date VARCHAR(20),account1 VARCHAR(10),amount2 int,init_date1 VARCHAR(10)) AS
-- 插入的数据
SELECT * from (
select * from tempa
union
SELECT * from tempb) b;
转载链接:https://www.cnblogs.com/iloverain/p/8862785.html
标准 SQL 里定义了外连接的三种类型,如下所示。
● 左外连接(LEFT OUTER JOIN)
● 右外连接(RIGHT OUTER JOIN)
● 全外连接(FULL OUTER JOIN)
MySQL是不支持“全外连接(FULL OUTER JOIN)”的,比如,下面的操作:(报错)
SELECT
COALESCE(A.stuno,B.stuno) AS stuno,
COALESCE(A.remarks,B.remarks ) AS remarks
FROM
clubcollect A FULL OUTER JOIN stuinfo B ON A.stuno = B.stuno
WHERE A.remarks IS NULL OR B.remarks IS NULL;
如果想要实现,请参考“全关联结果表”,未完,待续...