我有一張如下表:
表_a:
Pos_id col1 col2 value
12221 null null Car
12222 112 1111 Bike
12222 112 1112 Bus
12222 112 1113 Roller
表_b:
pos_id col1 col2 line_nr
12221 100 1000 1
12222 112 1111 1
12222 112 1112 2
12222 112 1113 3
我想在table_b中選擇line_nr為1的table_a的值。
所以我試過LEFT JOIN。
select * from table_a
left join table_b
on a.pos_id = b.pos_id
and a.col1 = b.col1
and a.col2 = b.col2
where b.line_nr = 1;
所以這不會選擇 table_a 中具有 null 的列。
我需要從 table_a 中選擇以下給定的列
Pos_id col1 col2 value
12221 null null Car
12222 112 1111 Bike
uj5u.com熱心網友回復:
您必須使用左外連接和帶有或的接受行line_nr = 1NULL
SELECT a.POS_ID, a.COL1, a.COL2, a.VAL
FROM table_a a
LEFT OUTER JOIN table_b b
ON a.pos_id = b.pos_id
AND a.col1 = b.col1
AND a.col2 = b.col2
where nvl(b.line_nr,1) = 1
;
POS_ID COL1 COL2 VAL
---------- ---------- ---------- ------
12222 112 1111 Bike
12221 Car
這可能是你的意思-讓行從a該要么匹配b和具有line_nr = 1 或不匹配(即line_nr is null)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324630.html
上一篇:這段代碼片段中的async/await是如何作業的?
下一篇:將表連接在一起
