SQL关联 NULL 值的处理

正常的 SQL 中,如果两张表关联,on t1.id=t2.id 那么会自动加上过滤条件 t1.id is not null and t2.id is not null。如果我们希望t1.id is null 的行和 t2.id is null 的行也进行关联的话,字段的比较用 <=> 关键字。如下例

create table t1(id string, value string);
insert into t1 values('1', 'v1'),(null,'v2');

create table t2(id string, value string);
insert into t2 values('1', 'v2'),(null,'v3');

select t1.id, t1.value,t2.id,t2.value 
from t1 join t2 on t1.id <=> t2.id;

结果如下:

t1_id t1_value t2_id t2_value
1 v1 1 v2
NULL v2 NULL v3

猜你喜欢

转载自blog.csdn.net/houzhizhen/article/details/133376978