我有这个简单的查询
select * from users where name = 'User1'
我想扩展查询的功能,无论查询返回0记录,查询都会通过其他子句获取数据。
where name = 'Default'
如果第一个子句将获取某些记录,则第二个子句将被忽略。
修改
的Oracle
答案 0 :(得分:7)
SELECT * FROM users WHERE name = 'User1'
UNION ALL
SELECT * FROM users WHERE name = 'Default'
AND NOT EXISTS (SELECT 1 FROM users WHERE name='User1')
答案 1 :(得分:4)
IF EXISTS (SELECT * from users where name = 'User1')
SELECT * from users where name = 'User1';
ELSE
SELECT * from users where name = 'Default';
答案 2 :(得分:1)
select top 1 * from users where name in ('User1', 'Default') order by name desc
:P
答案 3 :(得分:1)
WITH T AS (
SELECT users.*,
RANK() OVER (ORDER BY CASE WHEN name = 'User1' THEN 0 ELSE 1 END) AS RN
FROM users
WHERE name IN ( 'Default','User1')
)
SELECT * FROM T
WHERE RN = 1