Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to using select with if condition in oracle?

My requirement is to get a number from a complex query and check if the num = desiredNum.

If it is equal to desiredNum, then I must perform another set of select statements,

Is there any way I can achieve this in a query rather than writing a function?

Eg:

select case when val =2  
then select val1 from table1  
else 'false'  
from (select val from table)  

Is this possible ??

like image 346
swathy Avatar asked Mar 04 '11 04:03

swathy


1 Answers

select case when val=2 then val1 else val end as thevalue
from table1

I assume you meant that val and val1 are both from the same table, but when val=2, to use val1 instead. If you actually had two tables, and they both have only one record each, then

select
    case when val=2
    then (select val1 from table1)
    else 'false'
    end
from table
like image 121
RichardTheKiwi Avatar answered Sep 21 '22 22:09

RichardTheKiwi