我有一个表A和B,带有一个公共ID字段。我想在这些表上进行完全外连接,选择ID和“表A”或“表B”,具体取决于ID来自哪个表。
SELECT ID, ['tableA'|'tableB']
FROM A FULL OUTER JOIN B ON A.ID = B.ID
WHERE A.ID IS NULL OR B.ID IS NULL
答案 0 :(得分:1)
试试这个:
SELECT COALESCE(A.ID , B.ID) AS ID ,
CASE WHEN A.ID IS NOT NULL AND B.ID IS NULL THEN <tableA results>
WHEN A.ID IS NULL AND B.ID IS NOT NULL THEN <tableB results>
END AS results
FROM A FULL OUTER JOIN B ON A.ID = B.ID
WHERE A.ID IS NULL OR B.ID IS NULL
答案 1 :(得分:0)
所以这是我在Tab的一点帮助后想出来的。
SELECT ID, SELECT CASE WHEN ID IN (SELECT ID FROM A) THEN 'tableA'
WHEN ID IN (SELECT ID FROM B) THEN 'tableB'
FROM A FULL OUTER JOIN B ON A.ID = B.ID
WHERE A.ID IS NULL OR B.ID IS NULL