rathan
rathan

Reputation: 1

executing query1 if condition is true else query2

if(condition ,select t1.a,t1.b from t1 join t2 on t2.c=t1.a, select t1.a,t1.b from t1) 

but when am executing it shows error how to overcome and how to get result

Upvotes: 0

Views: 163

Answers (2)

Kedar Limaye
Kedar Limaye

Reputation: 1041

First of all mention your full SQL statement, Documentation for IF is

IF search_condition THEN statement_list
    [ELSEIF search_condition THEN statement_list] ...
    [ELSE statement_list]
END IF

Where is your select statement??

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270081

if() can be a function used within a SQL statement. I discourage this use because the standard construct is case.

if can also be a control flow construct. However, this is allowed only in programming blocks (think stored procedures and triggers).

Here are two ways to do what you want in a single query:

select t1.a, t1.b
from t1 join
     t2
     on t2.c = t1.a
where condition
union all
select t1.a, t1.b
from t1
where not (condition);

Or:

select t1.a, t1.b
from t1
where (not condition) or
      exists (select 1 from t2 where t2.c = t1.a);

Note that both of these assume that condition does not evaluate to NULL (although that is easily included in the logic).

Upvotes: 1

Related Questions