Search code examples
mysqlsqlcase-when

How to find Type of triangle(length of three sides are given) using nested case in mysql?


Sample Table

What Is wrobg in this MySQL query? It is showing syntax error.

SELECT
CASE
    WHEN A = B
        CASE
            WHEN A = C THEN 'Equilateral'
            WHEN C < A + B THEN 'Isosceles'
            ELSE 'Not A Triangle'
        END
    WHEN B = C
        CASE
            WHEN A < B + C THEN 'Isosceles'
            ELSE 'Not A Triangle'
        END
    WHEN A + B > C AND A + C > B AND B + C > A THEN 'Scalene'
    ELSE 'Not A Triangle'
END
FROM TRIANGLES;

Solution

  • You have to add also a then for that purpose

    SELECT
    CASE
        WHEN A = B
        THEN
            CASE
                WHEN A = C THEN 'Equilateral'
                WHEN C < A + B THEN 'Isosceles'
                ELSE 'Not A Triangle'
            END
        WHEN B = C
        THEN
            CASE
                WHEN A < B + C THEN 'Isosceles'
                ELSE 'Not A Triangle'
            END
        WHEN A + B > C AND A + C > B AND B + C > A THEN 'Scalene'
        ELSE 'Not A Triangle'
    END
    FROM TRIANGLES;