In MySQL, how to execute two different SELECT statements depending on a condition? SET @input1=123;
SET @input2='foobar';


IF @inout1&gt;0 THEN

SELECT
    @result := col12
FROM
    table1
WHERE
   col11=@input1;

ELSE IF @input2 != ''

SELECT
    @result := col21
FROM
    table2
WHERE
    col22=@input2;

END


Basically depending on if @input1 or @input2 is set and if the set value satisfy certain condition, one of the two SELECT statements should be run. But I just couldn't get it to work with IF THEN ELSE statement in MySQL. What should be the right syntax?
SELECT *
FROM ( full text of query 1 )
WHERE (condition 1)
UNION ALL
SELECT *
FROM ( full text of query 2 )
WHERE (condition 2)
  AND NOT (condition 1)


Neither condition must give NULL (due to NULL in field/variable values) in NOT.

Outer SELECTs for each separate subquery in UNION may be excess if separate queries are not complex.

For the code shown in the question it will be

SET @input1=123;
SET @input2='foobar';

SELECT @result := col12
FROM   table1
WHERE  col11 = @input1
  AND  @input1 &gt; 0

UNION ALL

SELECT @result := col21
FROM   table2
WHERE  col22=@input2
  AND  @input2 != ''
  AND  COALESCE(@input1, -1) &lt;= 0; /* prevent ejecting by NULL */