[LeetCode] 610. Triangle Judgement_Easy tag: SQL

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] 610. Triangle Judgement_Easy tag: SQL相关的知识,希望对你有一定的参考价值。

A pupil Tim gets homework to identify whether three line segments could possibly form a triangle.

 

However, this assignment is very heavy because there are hundreds of records to calculate.

 

Could you help Tim by writing a query to judge whether these three sides can form a triangle, assuming table triangle holds the length of the three sides x, y and z.

 

| x  | y  | z  |
|----|----|----|
| 13 | 15 | 30 |
| 10 | 20 | 15 |

For the sample data above, your query should return the follow result:

| x  | y  | z  | triangle |
|----|----|----|----------|
| 13 | 15 | 30 | No       |
| 10 | 20 | 15 | Yes      |

Code

1) use IF

SELECT*,
if (x+y>z and x+z>y and y+z>x, "Yes","No") as triangle
FROM triangle

 

2) use case

SELECT 
    x,
    y,
    z,
    CASE
        WHEN x + y > z AND x + z > y AND y + z > x THEN Yes
        ELSE No
    END AS triangle
FROM
    triangle

 

以上是关于[LeetCode] 610. Triangle Judgement_Easy tag: SQL的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode120 - Triangle

Leetcode 120

leetcode 120. Triangle

LeetCode120Triangle[DP]

leetcode 120 Triangle ----- java

LeetCode 之 Triangle