mysql select if else code example

Example 1: mysql select else if

select id, 
    (
    CASE 
        WHEN qty_1 <= '23' THEN price
        WHEN '23' > qty_1 && qty_2 <= '23' THEN price_2
        WHEN '23' > qty_2 && qty_3 <= '23' THEN price_3
        WHEN '23' > qty_3 THEN price_4
        ELSE 1
    END) AS total
 from product;

Example 2: mysql else if

IF condition1 THEN
    statements;
ELSEIF condition2 THEN # OPTIONAL
	statements;
ELSE # OPTIONAL
    statements;
END IF;

Example 3: mysql if else

IF condition THEN
   statements;
ELSE
   else-statements;
END IF;

Example 4: if inside select mysql

SELECT IF(1>3,'true','false');

Example 5: mysql use if on select

IF(expression ,expr_true, expr_false);

Tags:

Sql Example