Type of Triangle in MYSQL

前端 未结 18 2160
执念已碎
执念已碎 2020-12-09 11:48

Problem statement:

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the followin

18条回答
  •  时光说笑
    2020-12-09 12:42

    SELECT
      CASE 
        WHEN A + B <= C or A + C <= B or B + C <= A THEN 'Not A Triangle'
        WHEN A = B and B = C THEN 'Equilateral'
        WHEN A = B or A = C or B = C THEN 'Isosceles'
        WHEN A <> B and B <> C THEN 'Scalene'
      END tuple
    FROM TRIANGLES;
    
    1. By using case statement check if given input is a triangle or not.
    2. If it is a triangle then check if all sides are same if true the triangle type is 'Equilateral'.
    3. If not then check if any two sides are equal if true the triangle type is 'Isosceles'
    4. In the case of not equal, any sides the triangle type is 'Scalene'. We can directly use ELSE also.

提交回复
热议问题