Hey.
SQL queries aren't really meant to include that level of logic.
For things like that, you would be better of trying a
Stored Procedure
Something along the lines of:
- DELIMITER %%
-
CREATE PROCEDURE `Example`(IN pInput VARCHAR(64))
-
BEGIN
-
-- Declare all variables we plan on using
-
DECLARE doLoop BOOLEAN DEFAULT TRUE;
-
DECLARE cCondition VARCHAR(64);
-
-
-- Create query cursors, so we can read the results.
-
DECLARE queryCursor CURSOR FOR
-
SELECT `something`
-
FROM `parentTable`
-
WHERE `condition` = pInput;
-
-
-- Execute the query
-
OPEN queryCursor;
-
-
-- Fetch the first row of results
-
FETCH queryCursor INTO cCondition
-
-
-- Execute a query based on the results
-
IF cCondition == 'something' THEN
-
SELECT stuff FROM firstTable WHERE condition = pInput;
-
ELSE
-
SELECT stuff FROM secondTable WHERE condition = pInput;
-
END IF
-
-
-- Close the cursor
-
CLOSE queryCursor;
-
END%%
-
DELIMITER ;
And, once you have created the procedure, you call it in your code like so:
- CALL `Example`('input value');