Type of Triangle in MYSQL

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

    Your answer is actually right. But the one little information (Triangle's 'Right' Definition) is missing on the question (which is not your fault) that if two sides' sum is bigger OR EQUAL to another side then it's not a triangle

    The only thing you should do is putting '=' near of your '<'

    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 as triangles_type 
    from TRIANGLES; 
    
    0 讨论(0)
  • 2020-12-09 12:41

    MySQL With All Test Case Passed:-

    SELECT CASE 
    WHEN A + B > C AND A+C>B AND B+C>A THEN CASE 
    WHEN A = B AND B = C THEN 'Equilateral' 
    WHEN A = B OR B = C OR A = C THEN 'Isosceles' 
    WHEN A != B OR B != C OR A != C THEN 'Scalene' 
    END ELSE 'Not A Triangle' END FROM TRIANGLES;
    
    0 讨论(0)
  • 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.
    0 讨论(0)
  • 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"
    else "Scalene"
    end as triangles_type
    from TRIANGLES;
    
    0 讨论(0)
  • 2020-12-09 12:42
    select 
    case
        when (a+b>c) and (b+c>a) and (c+a>b) then
            case
                when a=b and b=c then 'Equilateral'
                when a=b or b=c or c=a then 'Isosceles'
                else 'Scalene'
            end
        else 'Not A Triangle'
    end 
    from triangles;
    
    0 讨论(0)
  • 2020-12-09 12:45

    For Oracle 11g this is working:

    SELECT CASE WHEN A+B>C then case when A=B AND B=C THEN 'Equilateral'
       WHEN A=B OR B=C OR A=C THEN 'Isosceles'
       WHEN A!=B or B!=C OR A!=C THEN 'Scalene' END
       ELSE 'Not A Triangle' END
       FROM TRIANGLES;
    
    0 讨论(0)
提交回复
热议问题