- 学习:知识的初次邂逅
- 复习:知识的温故知新
练习:知识的实践应用
目录
解法二, case......when......then...else...end解法
一,原题力扣链接
二,题干
表:
Triangle
+-------------+------+ | Column Name | Type | +-------------+------+ | x | int | | y | int | | z | int | +-------------+------+ 在 SQL 中,(x, y, z)是该表的主键列。 该表的每一行包含三个线段的长度。对每三个线段报告它们是否可以形成一个三角形。
以 任意顺序 返回结果表。
查询结果格式如下所示。
示例 1:
输入: Triangle 表: +----+----+----+ | x | y | z | +----+----+----+ | 13 | 15 | 30 | | 10 | 20 | 15 | +----+----+----+ 输出: +----+----+----+----------+ | x | y | z | triangle | +----+----+----+----------+ | 13 | 15 | 30 | No | | 10 | 20 | 15 | Yes | +----+----+----+----------+
三,建表语句
-- 判断三角形
Create table If Not Exists Triangle (x int, y int, z int);
Truncate table Triangle;
insert into Triangle (x, y, z) values ('13', '15', '30');
insert into Triangle (x, y, z) values ('10', '20', '15');
select * from Triangle;
四,分析
题解:
表:三角形表
字段:x,y,z 三个边长
要求:显示三个边长,并且新增一列,要求判断x,y,z能否组成三角形
解法:
三角形判断公式:任何两边之和大于第三边,任何两边之差小于第三边。
实际上,只需要判断任意两边之和大于第三边即可。
即代码:
判断 x+y>z and x+z>y y+z>x 即可sql解法思路一,if判断解法
sql解法思路二 case......when......then...else...end解法
五,SQL解答
解法一,if判断解法
select *,
if(x+y>z and x+z>y and y+z>x,'Yes','No') as triangle
from triangle;
解法二, case......when......then...else...end解法
select *,
(case
when x+y>z and x+z>y and y+z>x then 'Yes'
else 'No'
end) as triangle
from Triangle;
六,验证
七,知识点总结
- if判断语法练习;
- case then语法练习
- 三角形判断公式:任意两边之和大于第三边;
- 学习:知识的初次邂逅
- 复习:知识的温故知新
练习:知识的实践应用