not exists 与 select null,select 1的作用

简介: 1. 构建虚拟表 create table tmp01 as with tmp as ( select 1 as id from dual union all select ...

1. 构建虚拟表

 create table tmp01 as 
with tmp as (
  select 1 as id from dual  union all
  select 2       from dual  union all
  select 3       from dual  union all
  select null    from dual
)
select * from tmp;

create table tmp02 as 
with tmp as (
  select 1 as id from dual  union all
  select 2       from dual  union all
  select null    from dual
)
select * from tmp;

select id
from tmp01 
where id not in ( select id from tmp02 );

2. 在使用not exitis常常碰到类似的查询not exists ( select 1 from tmp02 where tmp02.id=tmp01.id )或者not exists ( select null from tmp02 where tmp02.id=tmp01.id ),其实它们的结果是一样的,都是返回t1表与t2表的差集

SQL> select id,CASE  WHEN  ID IS NULL THEN 'id is null' ELSE 'id is not null' END "id null" from tmp01 where not exists ( select 1 from tmp02 where tmp02.id=tmp01.id );
        ID id null
---------- --------------
           id is null
         3 id is not null
SQL> select id,CASE  WHEN  ID IS NULL THEN 'id is null' ELSE 'id is not null' END "id null" from tmp01 where not exists ( select NULL from tmp02 where tmp02.id=tmp01.id );
        ID id null
---------- --------------
           id is null
         3 id is not null
SQL> select id,CASE  WHEN  ID IS NULL THEN 'id is null' ELSE 'id is not null' END "id null" from tmp01 where not exists ( select 'c' from tmp02 where tmp02.id=tmp01.id );
        ID id null
---------- --------------
           id is null
         3 id is not null


目录
相关文章
|
1月前
|
Java
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
49 1
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
Error:(15, 13) java: No property named “id” exists in source parameter(s). Did you mean “null”?
MapStruct - No property named “XXX“ exists in source parameter(s). Did you mean “null“?
MapStruct - No property named “XXX“ exists in source parameter(s). Did you mean “null“?
870 0
|
Oracle 关系型数据库 MySQL
关于ORACLE MYSQL NOT IN和NOT exists需要注意的 NULL值
首先说明NOT IN 和NOT EXISTS 并不完全等价 ORACLE MYSQL 都是如此 源表: SQL> select * from testa1; NAME                          ID ---------------...
858 0
|
SQL 测试技术
not in/not exists 的 null 陷阱
以前遇到了 not in 子查询的一个 null 陷阱,有经验的朋友可能知道怎么回事了,用代码简单说明一下: -- 创建两张测试表: create table tmp01 as  wi...
711 0
|
1月前
|
机器学习/深度学习 SQL 关系型数据库
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
【MySQL进阶之路丨第十一篇】一文带你精通MySQL NULL值处理、正则表达式
51 0
|
1月前
|
SQL 关系型数据库 MySQL
总结 vue3 的一些知识点:MySQL NULL 值处理
总结 vue3 的一些知识点:MySQL NULL 值处理
|
1月前
|
SQL 关系型数据库 MySQL
实时计算 Flink版产品使用合集之从MySQL同步数据到Doris时,历史数据时间字段显示为null,而增量数据部分的时间类型字段正常显示的原因是什么
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStreamAPI、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
1月前
|
SQL 关系型数据库 MySQL
python在mysql中插入或者更新null空值
这段代码是Python操作MySQL数据库的示例。它执行SQL查询从表`a_kuakao_school`中选取`id`,`university_id`和`grade`,当`university_id`大于0时按升序排列。然后遍历结果,根据`row[4]`的值决定`grade`是否为`NULL`。若不为空,`grade`被格式化为字符串;否则,设为`NULL`。接着构造UPDATE语句更新`university`表中对应`id`的`grade`值,并提交事务。重要的是,字符串`NULL`不应加引号,否则更新会失败。
56 2

热门文章

最新文章

相关实验场景

更多