码迷,mamicode.com
首页 > 数据库 > 详细

[LeetCode] 610. Triangle Judgement_Easy tag: SQL

时间:2018-08-29 01:14:28      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:else   ret   sig   length   res   home   from   form   min   

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

标签:else   ret   sig   length   res   home   from   form   min   

原文地址:https://www.cnblogs.com/Johnsonxiong/p/9551781.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!