user1474111
user1474111

Reputation: 1516

if else in where clause on Oracle

I need a query but I have a criteria depends on situation. As an example :

Lets have a table named table1 that has column named column1 and table2 that has column named column2

In the query in where clause, I need to join this table1 with table2 by column1 and column2 columns. But, I need to do like

if 
     column1 = 'x' then my criteria must be column1 = column2
else
     column1 != 'x'

How I can write this where clause in Oracle?

Thank you for your answer.

Upvotes: 0

Views: 7754

Answers (2)

Himanshu
Himanshu

Reputation: 32612

Just use OR condition like this one:

WHERE (Column1 <> 'x' OR column1 = column2)

(This is normal SQL syntax. Don't know if there is any change in syntax of Oracle. But you can try this logic)

EDIT: (From OP's comment in my answer)

You can do that something like this one:

WHERE (column2 != (Select col from table3) OR column1 = 'x')

Or

WHERE (column2 != (Select col from table3) OR column1 = (Select col from table3))

Upvotes: 2

gdanton
gdanton

Reputation: 320

Try this

WHERE (Column1 != 'x' OR (Column1 = 'x' AND column1 = column2))

Upvotes: 2

Related Questions