Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditionally sql query

I have this simple query

select * from users where name = 'User1'

I'd like to extend the query's functionality whatever the query returns 0 records the query will fetch the data by other clause.

where name = 'Default'

If the first clause will fetch some records the second clause will be ignored.

EDIT

Oracle

like image 944
user137348 Avatar asked May 14 '26 09:05

user137348


1 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')
like image 161
mdma Avatar answered May 17 '26 02:05

mdma