我有表A:
ColA | ColB
A | 0
B | 0
C | 0
和表B:
ColA | ColB
A | 10
D | 50
我想使用LEFT OUTER JOIN,如果右表的行存在,那么它将使用右表,否则使用左表。我想要的结果是:
ColA | ColB
A | 10
B | 0
C | 0
答案 0 :(得分:2)
SELECT
a.ColA,
ISNULL(b.ColB, 0) AS B_ColB
FROM dbo.TableA a
LEFT JOIN dbo.TableB b ON a.ColA = b.ColA
答案 1 :(得分:1)
SELECT
a.ColA,
ISNULL(b.ColB, a.ColB) AS B_ColB
FROM dbo.TableA a
LEFT JOIN dbo.TableB b ON a.ColA = b.ColA
<强> FIDDLE 强>
答案 2 :(得分:0)
你可以在这里使用Full outer join ...
这是一个例子
SELECT Customers.CustomerName, Orders.OrderID
FROM Customers
FULL OUTER JOIN Orders
ON Customers.CustomerID=Orders.CustomerID