Q:

writing conditional select statements

What is the MySQL syntax for the pseudo code below?
    Integer x = 0;
    String y = "ABC";

    IF (x equals 0) then
    SELECT * FROM table_x
    ELSE 
    SELECT * FROM table_y

A:

You can use UNION ALL with two queries; one only returning rows when x is 0; the other only returning rows when x is not 0:
SET @x = 0;

SELECT * FROM table_x where @x = 0
UNION ALL
SELECT * FROM table_y where @x <> 0;

This only works when columns are the same in the two tables. Use column names instead of * to make sure to select the same set of columns. If you want to select different columns from the two tables (number or types of columns differ), then you cannot write one query, because a query must always return the same number and types of columns.
SELECT dept_no, dept_name FROM table_x where @x = 0
UNION ALL
SELECT department_number, name FROM table_y where @x <> 0;

