0

例子

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 中的任何内容中。感谢任何帮助。下划线只是为了使间距一致

4

2 回答 2

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)
于 2013-06-24T17:25:37.353 回答
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
于 2013-06-24T17:22:19.837 回答