简单连接一样,当第一个表中的行与第二个表中的相应(按顺序)行匹配时,没有任何条件。
作为一种解决方法,我想出了这个:
SELECT value, "table1" AS src FROM t1
UNION ALL
SELECT value, "table2" AS src FROM t2
给出:
value
列用于使用
ROW_NUMBER()
添加顺序。
WITH table1_with_rownum AS (
SELECT value, ROW_NUMBER() OVER (ORDER BY value) AS rownum
FROM table1
),
table2_with_rownum AS (
SELECT value, ROW_NUMBER() OVER (ORDER BY value) AS rownum
FROM table2
)
SELECT
t1.value AS value1,
t2.value AS value2
FROM table1_with_rownum t1
LEFT JOIN table2_with_rownum t2 ON t1.rownum = t2.rownum
UNION
SELECT
t1.value AS value1,
t2.value AS value2
FROM table1_with_rownum t1
RIGHT JOIN table2_with_rownum t2 ON t1.rownum = t2.rownum
ORDER BY value1;
输出