Type of Triangle in MYSQL

前端 未结 18 2156
执念已碎
执念已碎 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:18

    Give it a try. That should definitely work.

    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'
        ELSE 'Scalene'
      END
    FROM TRIANGLES;

    0 讨论(0)
  • 2020-12-09 12:18

    For MySQL:

    select
      case
          when (a+b>c) and (a+c>b) and (b+c>a) then
               case
                  when (a=b) and (b=c) and (a=c) then 'Equilateral'
                  when (a=b) or (b=c) or (a=c) then 'Isosceles'
                  else 'Scalene'
               end
           else 'Not A Triangle'
        end    
    from TRIANGLES 
    
    0 讨论(0)
  • 2020-12-09 12:19
    SELECT CASE
    WHEN (A + B <= C) OR (B+C <= A) OR (A+C <= B) THEN "Not A Triangle" 
    WHEN (A=B) AND (B=C) THEN "Equilateral"
    WHEN (A=B) OR (C=A) OR (B=C) THEN "Isosceles" 
    ELSE "Scalene" 
    END 
    FROM TRIANGLES
    
    0 讨论(0)
  • 2020-12-09 12:21

    MySQL:

    select
    case 
        when ((A+B)<=C) || ((C+B)<=A) || ((A+C)<=B)  then 'Not A Triangle'
        when A=B && B=C && A=C then 'Equilateral'
        when ((A=B) && (A+B)>C) || ((B=C) && (B+C)>A)  then 'Isosceles'    
        when A!=B && B!=C && A!=C then 'Scalene' 
        end
    from TRIANGLES;
    

    Explanation:

    First, check whether the values satisfies the requirement of a triangle(i.e sum of two sides must be greater than the other side) - Not A Triangle

    Then, look for the type of the triangle

    1. Equilateral - All sides are equal
    2. Isosceles - Any two sides are equal(included additional condition of sum two sides greater than the other side - this can be ignored as we check the fundamental rule of a triangle as a first case)
    3. Scalene - All three sides are different
    0 讨论(0)
  • 2020-12-09 12:22

    select case when a=b and b=c then 'Equilateral' when a+b <=c or b+c<=a or a+c

    0 讨论(0)
  • 2020-12-09 12:24

    /* THE BELOW CODE WILL CHECK ALL THE POSSIBLE CASES OF A TRIANGLE, THIS CODE WILL WORK 100% */

    select case
    when A+B<=C OR A+C<=B OR C+B<=A THEN "Not A Triangle"
    when (A<>B AND A+B>C) AND (B<>C AND B+C>A) AND (A<>C AND A+C>B) THEN 
    "Scalene"
    when (A=B AND B<>C AND A+B>C) OR (B=C AND B<>A AND B+C>A) OR (A=C AND C<>B 
    AND A+C>B) THEN "Isosceles"
    when A=B AND B=C THEN "Equilateral"
    end as triangle
    from TRIANGLES;
    
    0 讨论(0)
提交回复
热议问题