Skip to content

Latest commit

 

History

History
55 lines (35 loc) · 1.08 KB

0610._Triangle_Judgement.md

File metadata and controls

55 lines (35 loc) · 1.08 KB

610. Triangle Judgement

难度: Easy

刷题内容

原题连接

内容描述


SQL Schema
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      |

解题方案

思路 1

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

思路 2

select *, if(x + y > z and x + z > y and y + z > x, 'Yes', 'No') as triangle from triangle