kinkajou
kinkajou

Reputation: 3728

Conditional MySql Query

I have a situation where I need to execute two different queries in MySql is it possible to do so in MySql. It is select query.

 eg. Query1:  Select * from table1
     Query2: Select * from table2

Now how do I execute these query

conditonally

select * from if(somecondition ) then Query 1 else query 2

Upvotes: 4

Views: 2907

Answers (2)

Brian Webster
Brian Webster

Reputation: 30855

IF MyCondition = True THEN
  Select * from table1;
ELSE
  Select * from table2;
END IF;

Reference

Upvotes: 3

Karan Shah
Karan Shah

Reputation: 752

i think this can be done using union , i do have a small example to share hope it helps...

Example:

SELECT TEXT,language 
FROM TABLE 
WHERE LANGUAGE = 'spanish'
union all
select text,language
from TABLE as t
where language = 'english'
and not exists
(select *
 from table
 where language = 'spanish'
 and table.pid = t.pid)

Upvotes: 0

Related Questions