MySQL实现全关联 full outer join
SQL LEFT JOIN 关键字
LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有匹配的行。
LEFT JOIN 关键字语法
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。
SQL RIGHT JOIN 关键字
RIGHT JOIN 关键字会右表 (table_name2) 那里返回所有的行,即使在左表 (table_name1) 中没有匹配的行。
RIGHT JOIN 关键字语法
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。
SQL FULL JOIN 关键字
只要其中某个表存在匹配,FULL 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;
3.后续操作,计算amount1 + amount2
SELECT IFNULL(account,account1),IFNULL(init_date,init_date1), IFNULL(amount1,0)+IFNULL(amount1,0) AS total from tempc where (account=account1) and (init_date=init_date1) ;
本文版权归作者所有,转载请注明出处http://www.cnblogs.com/iloverain/.未经作者同意必须保留此段声明,否则保留追究法律责任的权利.