左连接在Oracle连接列与空值



我有一个像下面这样的表格:

Table_a:

Pos_id    col1     col2   value
12221     null     null    Car
12222     112      1111    Bike
12222     112      1112    Bus
12222     112      1113    Roller

Table_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中为空的列

我需要从table_a

中选择以下给定的列
Pos_id    col1     col2   value
12221     null     null    Car
12222     112      1111    Bike

必须使用左外连接accept行与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)

最新更新