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 ('', 210, '');
INSERT INTO `data_stock1` VALUES ('', 70, '');
INSERT INTO `data_stock1` VALUES ('', 200, '');
INSERT INTO `data_stock1` VALUES ('', 30, '');
INSERT INTO `data_stock1` VALUES ('', 10, ''); drop table if exists data_stock1;
create table data_stock (account varchar(10),amount2 int,init_date varchar(20));
INSERT INTO `data_stock2` VALUES ('', 70, '');
INSERT INTO `data_stock2` VALUES ('', 200, '');
INSERT INTO `data_stock2` VALUES ('', 30, '');
INSERT INTO `data_stock2` VALUES ('', 10, '');
INSERT INTO `data_stock2` VALUES ('', 30, '');

  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;

MySQL实现全关联 full outer join

MySQL实现全关联 full outer join

MySQL实现全关联 full outer join     

  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) ;

MySQL实现全关联 full outer join

上一篇:2014 Multi-University Training Contest 6


下一篇:树递归写法ref实现