SQLite:四个表的完全外部连接
SQLite: full outer join with four tables
编辑:可以找到这个问题的扩展
我想用 SQLite 连接四个不同的表,它们只有两列相同。请看下面的例子
+--------+---+-----+-----+
| table1 | | | |
+--------+---+-----+-----+
| a | b | lon | lat |
+--------+---+-----+-----+
| 1 | 2 | 111 | 222 |
+--------+---+-----+-----+
+--------+---+-----+-----+
| table2 | | | |
+--------+---+-----+-----+
| c | d | lon | lat |
+--------+---+-----+-----+
| 3 | 4 | 333 | 444 |
+--------+---+-----+-----+
+--------+---+-----+-----+
| table3 | | | |
+--------+---+-----+-----+
| e | f | lon | lat |
+--------+---+-----+-----+
| 5 | 6 | 555 | 666 |
+--------+---+-----+-----+
+--------+---+-----+-----+
| table4 | | | |
+--------+---+-----+-----+
| g | h | lon | lat |
+--------+---+-----+-----+
| 7 | 8 | 777 | 888 |
+--------+---+-----+-----+
这些表没有通过任何外键连接。此外,每一行的 lon/lat 值都不同。最佳输出将是:
+------+------+------+------+------+------+------+------+-----+-----+
| a | b | c | d | e | f | g | h | lon | lat |
+------+------+------+------+------+------+------+------+-----+-----+
| None | None | 3 | 4 | None | None | None | NOne | 333 | 444 |
| 1 | 2 | None | None | None | None | None | None | 111 | 222 |
| None | None | None | None | 5 | 6 | None | None | 555 | 666 |
| None | None | None | None | None | None | 7 | 8 | 777 | 888 |
+------+------+------+------+------+------+------+------+-----+-----+
- 同样,这是我的最终目标 - lat/lon 值在表之间没有相互关联:
- 一个 |乙 | c | .. |纬度|经度 |代替
- 一个|乙 | c | .. | table1.lat | table2.lat | ...
- 感谢您的帮助!
- 我当前的代码
-- First two tables
CREATE VIEW ab AS
SELECT * FROM table1 LEFT JOIN table2 ON ???
UNION ALL
SELECT * FROM table2 LEFT JOIN table1 ON ?? WHERE ?? IS NULL
-- 3rd and 4th table
CREATE VIEW cd AS
SELECT * FROM table3 LEFT JOIN table4 ON ??
UNION ALL
SELECT * FROM table4 LEFT JOIN table3 ON ?? WHERE ?? IS NULL
-- -- JOIN
SELECT * FROM cd LEFT JOIN ab ON ??
UNION ALL
SELECT * FROM cd LEFT JOIN ab ON ?? WHERE ?? IS NULL
假设您的示例表具有如下数据
表 1:
a b lon lat
---------------
22 33 11 22
表 2:
c d lon lat
---------------
1 2 44 45
表3
e f lon lat
-----------------------
NULL NULL 100 101
表4
g h lon lat
-----------------------
NULL NULL 200 201
而你想合并记录,可以使用union all
。
select a,b,NULL as c, NULL as d,NULL as e, NULL as f, NULL as g, NULL as h, lon,lat
from table1
union all
select NULL, NULL,c,d,NULL as e, NULL as f, NULL as g, NULL as h, lon,lat
from table2
union all
select NULL, NULL,NULL,NULL,e,f, NULL as g, NULL as h, lon,lat
from table3
union all
select NULL, NULL,NULL,NULL,NULL,NULL,g,h, lon,lat
from table4
结果:
+------+------+------+------+------+------+------+------+-----+-----+
| a | b | c | d | e | f | g | h | lon | lat |
+------+------+------+------+------+------+------+------+-----+-----+
| 22 | 33 | NULL | NULL | NULL | NULL | NULL | NULL | 11 | 22 |
| NULL | NULL | 1 | 2 | NULL | NULL | NULL | NULL | 44 | 45 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 100 | 101 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 200 | 201 |
+------+------+------+------+------+------+------+------+-----+-----+
编辑:可以找到这个问题的扩展
我想用 SQLite 连接四个不同的表,它们只有两列相同。请看下面的例子
+--------+---+-----+-----+
| table1 | | | |
+--------+---+-----+-----+
| a | b | lon | lat |
+--------+---+-----+-----+
| 1 | 2 | 111 | 222 |
+--------+---+-----+-----+
+--------+---+-----+-----+
| table2 | | | |
+--------+---+-----+-----+
| c | d | lon | lat |
+--------+---+-----+-----+
| 3 | 4 | 333 | 444 |
+--------+---+-----+-----+
+--------+---+-----+-----+
| table3 | | | |
+--------+---+-----+-----+
| e | f | lon | lat |
+--------+---+-----+-----+
| 5 | 6 | 555 | 666 |
+--------+---+-----+-----+
+--------+---+-----+-----+
| table4 | | | |
+--------+---+-----+-----+
| g | h | lon | lat |
+--------+---+-----+-----+
| 7 | 8 | 777 | 888 |
+--------+---+-----+-----+
这些表没有通过任何外键连接。此外,每一行的 lon/lat 值都不同。最佳输出将是:
+------+------+------+------+------+------+------+------+-----+-----+
| a | b | c | d | e | f | g | h | lon | lat |
+------+------+------+------+------+------+------+------+-----+-----+
| None | None | 3 | 4 | None | None | None | NOne | 333 | 444 |
| 1 | 2 | None | None | None | None | None | None | 111 | 222 |
| None | None | None | None | 5 | 6 | None | None | 555 | 666 |
| None | None | None | None | None | None | 7 | 8 | 777 | 888 |
+------+------+------+------+------+------+------+------+-----+-----+
- 同样,这是我的最终目标 - lat/lon 值在表之间没有相互关联:
- 一个 |乙 | c | .. |纬度|经度 |代替
- 一个|乙 | c | .. | table1.lat | table2.lat | ...
- 感谢您的帮助!
- 我当前的代码
-- First two tables
CREATE VIEW ab AS
SELECT * FROM table1 LEFT JOIN table2 ON ???
UNION ALL
SELECT * FROM table2 LEFT JOIN table1 ON ?? WHERE ?? IS NULL
-- 3rd and 4th table
CREATE VIEW cd AS
SELECT * FROM table3 LEFT JOIN table4 ON ??
UNION ALL
SELECT * FROM table4 LEFT JOIN table3 ON ?? WHERE ?? IS NULL
-- -- JOIN
SELECT * FROM cd LEFT JOIN ab ON ??
UNION ALL
SELECT * FROM cd LEFT JOIN ab ON ?? WHERE ?? IS NULL
假设您的示例表具有如下数据
表 1:
a b lon lat
---------------
22 33 11 22
表 2:
c d lon lat
---------------
1 2 44 45
表3
e f lon lat
-----------------------
NULL NULL 100 101
表4
g h lon lat
-----------------------
NULL NULL 200 201
而你想合并记录,可以使用union all
。
select a,b,NULL as c, NULL as d,NULL as e, NULL as f, NULL as g, NULL as h, lon,lat
from table1
union all
select NULL, NULL,c,d,NULL as e, NULL as f, NULL as g, NULL as h, lon,lat
from table2
union all
select NULL, NULL,NULL,NULL,e,f, NULL as g, NULL as h, lon,lat
from table3
union all
select NULL, NULL,NULL,NULL,NULL,NULL,g,h, lon,lat
from table4
结果:
+------+------+------+------+------+------+------+------+-----+-----+
| a | b | c | d | e | f | g | h | lon | lat |
+------+------+------+------+------+------+------+------+-----+-----+
| 22 | 33 | NULL | NULL | NULL | NULL | NULL | NULL | 11 | 22 |
| NULL | NULL | 1 | 2 | NULL | NULL | NULL | NULL | 44 | 45 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 100 | 101 |
| NULL | NULL | NULL | NULL | NULL | NULL | NULL | NULL | 200 | 201 |
+------+------+------+------+------+------+------+------+-----+-----+