实施例
Table 1
W__ | F__ | C__ | Code
15__ 4___ 35__ A0
5___ 1___ 2___ A5
8___ 3___ 7___ A1
Table 2
W__ | F__ | C__ | Code
15__ 4___ 35__ NULL
2___ 3___ 6___ NULL
Desired output
15, 4, 35, A0
5, 1, 2, A5
8, 3, 7, A1
2, 3, 4, NULL
我想从表1中选择所有行,并将表2中所有不包含匹配的W,F和C列的行追加到表1中的任何行。任何帮助都表示赞赏。下划线只是为了使间距保持一致
答案 0 :(得分:2)
您可以将UNION与NOT EXISTS结合使用:
Select * from Table1
UNION ALL
Select * from Table2 WHERE NOT EXISTS (SELECT * FROM Table1
WHERE Table1.w = Table2.w
AND Table1.f = Table2.f
AND Table1.c = Table2.c)
答案 1 :(得分:1)
SELECT w,f,c,code
FROM [table 1]
UNION ALL
SELECT t2.w,t2.f,t2.c,t2.code
FROM [table 2] t2
LEFT OUTER JOIN [table 1] t1
ON t2.w = t1.w and t2.f = t1.f and t2.c = t1.c
WHERE t1.code is null