w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
MySQL Case Select not behaving as expected

You're mixing the two ways to use CASE. You either write:

CASE
    WHEN <expression1> THEN <result1>;
    WHEN <expression2> THEN <result2>;
    ...
END CASE

This evaluates each expression, and executes the corresponding result for the first true one. Or:

CASE <expression>
    WHEN <val1> THEN <result1>;
    WHEN <val2> THEN <result2>;
    ...
END CASE

This compares <expression> to each value, and executes the corresponding result for the first one that matches.

You used the second syntax, but your values also contain a comparison. So they're all either 0 (for false) or 1 (for true), and that's what you're comparing modTemp to. Change to:

    CASE modTemp
        WHEN 1 THEN
            SELECT 1;
        WHEN 2 THEN
            SELECT 2;
        WHEN 0 THEN
            SELECT 3;
        ELSE
            SELECT CONCAT('Error: modTemp = ', modTemp);
    END CASE;




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.