我想编写一个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或任何编程块
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