思路

利用只有一行的表左关联或有关联强制生成一条null记录

select 需要查询的数据
from 只有一行的表
left join 查询的表
on 1=1;

  

各数据库获取只有一行的表的方法

  • mysql
select 1;
  • oracle
dual

  

实例

编写一个 SQL 查询,获取 person 表中age为30的id,如果不存在age为30,那么查询应返回 null 。

id age
1 20
  • mysql
select t2.id
from ( select 1) t1
left join (
select id 
from person
where 
age = 30) t2
on 1 = 1;

  

  • oracle
select t1.id
from dual
left join (
select id 
from person
where 
age = 30) t1
on 1 = 1;

  

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-12-01
  • 2022-01-16
  • 2022-12-23
  • 2021-06-23
  • 2022-12-23
  • 2021-12-14
猜你喜欢
  • 2021-08-29
  • 2022-01-26
  • 2021-07-08
  • 2021-10-18
  • 2022-12-23
  • 2022-12-23
  • 2021-08-18
相关资源
相似解决方案