是否可以在一个表中比较两个表的数据?
表1
ID | Val |
---|---|
1 | Ann |
2 | 本 |
您可以使用完全联接。这大致是一个左和一个右连接在一起。它保留了两个表中的所有行。如果有匹配的伙伴行,则将合并伙伴行,如果没有,则伙伴行的值保持为NULL
。
SELECT coalesce(t1.id, t2.id) AS id,
t1.val AS val1,
t2.val AS val2,
CASE
WHEN t1.val = t2.val
OR t1.val IS NULL
AND t2.val IS NULL THEN
1
ELSE
0
END AS match
FROM "table 1" AS t1
FULL JOIN "table 2" as t2
ON t2.id = t1.id;