首页 > 解决方案 > 如何在不使用 Exists 关键字的情况下编写等效查询,以便将其转换为关系代数?

问题描述

我有一个查询:

    select daa.*
    from Dataa daa
    where not exists (select row() from Stuff)

基本上,当 Stuff 中有数据时,查询不会返回任何内容。但是,当 Stuff 为空时,查询将返回 Dataa 中的所有内容。我需要用相同的行为重写这个查询,但没有 exists 关键字,以便我可以将它翻译成关系代数。

我看过的一些资源是:

将 NOT IN 转换为 NOT EXISTS http://mlwiki.org/index.php/Translating_SQL_to_Relational_Algebra#EXISTS_in_the_Where_Clause_.28by_example.29

标签: sqlpostgresql

解决方案


没有几种方法可以编写它Exists

select daa.*
from Dataa daa
where (select 1 from Stuff limit 1) is null

select daa.*
from Dataa daa
where (select count(*) from Stuff) = 0

select daa.*
from Dataa daa
left outer join (select 1 one1 from Stuff)j on 1=1
where j.one1 is null

但我看不出该关键字到底有什么问题。


推荐阅读