Sql conditional query

I have this simple request

select * from users where name = 'User1'

I would like to expand the functionality of the query, as soon as the query returns 0 records, the query will retrieve data for another sentence.

where name = 'Default'

If the first sentence retrieves some records, the second sentence will be ignored.

EDIT

Oracle

+3
source share
4 answers
SELECT * FROM users WHERE name = 'User1'
UNION ALL 
SELECT * FROM users WHERE name = 'Default' 
    AND NOT EXISTS (SELECT 1 FROM users WHERE name='User1')
+7
source
 IF EXISTS (SELECT * from users where name = 'User1')
   SELECT * from users where name = 'User1';
 ELSE
   SELECT * from users where name = 'Default';
+4
source
select top 1 * from users where name in ('User1', 'Default') order by name desc

: R

+1
source
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
+1
source

Source: https://habr.com/ru/post/1760035/


All Articles