条件MySql查询

时间:2011-07-11 09:07:46

标签: mysql database

我有一种情况,我需要在MySql中执行两个不同的查询是否可以在MySql中执行此操作。这是选择查询。

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

现在我该如何执行这些查询

conditonally

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

2 个答案:

答案 0 :(得分:3)

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

Reference

答案 1 :(得分:0)

我认为这可以使用union来完成, 我确实有一个小例子来分享希望它有帮助...

示例:

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)