pirox22
pirox22

Reputation: 922

Yes or No as result of sql query without IF or CASE usage

Is there a way to make a query and return 'yes' or 'no' as an answer without the use of Flow Control operators?

The only solution I thought is:

select 'yes' as answer 
from blabla
where blabla

but this of course works only if the query found some results.

Upvotes: 1

Views: 3550

Answers (2)

Alex
Alex

Reputation: 17289

I like your blabla but don't understand your goal.

What is wrong with IF() statement or CASE WHEN ELSE END?

If you like your blabla code you can continue the same logic:

select 'yes' as answer 
from blabla
where blabla.column=1
UNION
select 'no' as answer 
from blabla
where blabla.column<>1

and here is another tricky solution:

http://sqlfiddle.com/#!9/76065/24

SELECT @answer
FROM (
  SELECT @answer:='yes' 
  FROM blabla
  WHERE blabla.id=5
) y
RIGHT JOIN (SELECT @answer:='no') n
ON 1; 

Upvotes: 6

Motomotes
Motomotes

Reputation: 4237

You may want to COALESCE:

select coalesce(
    select 'yes' as answer 
    from blablaT
    where blabla,
    select 'no' as answer 
    from blablaT
    where not blabla) as result from dual;

Upvotes: 0

Related Questions