MySQL IF

MySQL IF

MySQL IF syntax and example.

MySQL IF syntax

IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] …
[ELSE statement_list]
END IF

IF example

CREATE OR REPLACE FUNCTION
if_test(p_id int)
RETURNS TEXT
BEGIN
DECLARE v_return TEXT;
	IF p_id=1 THEN 
		SET v_return = 'ABC';
	ELSEIF p_id=2 THEN	
		SET v_return = 'XYZ';
	ELSE
		SET v_return='N/A';
	END IF;
RETURN v_return;
END;

SELECT if_test(1);

Output

FUNCTION IF_TEST compiled
ABC