如果存在,则显示匹配记录的SQL显示所有记录

use*_*436 0 sql oracle

我想编写一个SQL查询来显示匹配的行(如果存在),否则显示所有记录.

示例: - 输入数据: -

ID

1

2

3

4

select * from table where id = 2 /* since id exits it should return id o/p 2 */


select * from table where id = 8/* since id doesn't exist it should return all the rows of table) 
Run Code Online (Sandbox Code Playgroud)

即o/p

1

2

3

4

我想在SQL中严格执行此操作,不需要PL/SQL或任何编程块

evp*_*vpo 5

select *
from table
where not exists (select id from table where id = 2) or id = 2
Run Code Online (Sandbox Code Playgroud)

如果子查询未返回任何记录,则表中的所有记录的第一个条件为true,并且查询将返回所有记录.否则它是假的,我们只返回第二个条件为真的记录,即id = 2

  • +1...但是你应该学会在你的答案中格式化代码,并且添加一些解释并没有什么坏处(对于提出问题的人来说,看起来很明显可能不那么明显). (2认同)