我有两张桌子:
表1:
Order_No | Item_No | Line_No | Amount
---------|---------|---------|-------
121212 | AAAA | 1 | 500
151515 | BBBB | 1 | 400
151515 | CCCC | 2 | 900
表2:
Order_No | Item_No | Line_No | Invoice_No
---------|---------|---------|-----------
121212 | AAAA | 1 | 11000012
151515 | BBBB | 1 | 11002356
151515 | BBBB | 1 | 11000586
我想用左连接加入2个表(表1左连接表2)并返回表2的第一个匹配,如下面的结果:
结果:
Order_No | Item_No | Line_No | Invoice_No | Amount
---------|---------|---------|----------- |-------
121212 | AAAA | 1 | 11000012 | 500
151515 | BBBB | 1 | 11002356 | 400
151515 | CCCC | 2 | | 900
我该怎么做?
答案 0 :(得分:0)
嗯,它实际上不是"离开外连接":
SELECT
t1.Order_No, t1.Item_No, t1.Line_No,
(SELECT Invoice_No FROM Table2 AS t2
WHERE t1.Order_No=t2.Order_No AND t1.Item_No=t2.Item_No
AND t1.Line_No=t2.LineNo LIMIT 1),
Amount
FROM Table1 AS t1