您的位置:首页 > 文旅 > 旅游 > MySQL基础练习题47-判断三角形

MySQL基础练习题47-判断三角形

2024/10/6 8:27:57 来源:https://blog.csdn.net/weixin_58305115/article/details/141288332  浏览:    关键词:MySQL基础练习题47-判断三角形

目录

题目

准备数据

分析数据

方法一 :if函数

方法二:case when


题目

对每三个线段报告它们是否可以形成一个三角形。

准备数据

## 创建库
create database db;
use db;## 创建表
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')

分析数据

三角形构成原理是任意两条边之和大于第三条边,所以使用and运算符

方法一 :if函数

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

 方法二:case when

select x,y,z,casewhen x+y>z and x+z>y and y+z>x then 'Yes'else 'No'end triangle
from Triangle;

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com