mysql - MySQL中的三角形类型

标签 mysql

问题陈述:

Write a query identifying the type of each record in the TRIANGLES table using its three side lengths. Output one of the following statements for each record in the table:

  • Not A Triangle: The given values of A, B, and C don't form a triangle.
  • Equilateral: It's a triangle with sides of equal length.
  • Isosceles: It's a triangle with sides of equal length.
  • Scalene: It's a triangle with sides of differing lengths. Input Format

The TRIANGLES table is described as follows:

Each row in the table denotes the lengths of each of a triangle's three sides.

Sample Input
------------
A  B  C
20 20 23
20 20 20
20 21 22
13 14 30

Sample Output
-------------
Isosceles
Equilateral
Scalene
Not A Triangle

失败的尝试:

select
    case
        when A+B < C or A+C < B or B+C < A then "Not A Triangle"
        when A=B and B=C then "Equilateral"
        when A=B or A=C or B=C then "Isosceles"
        when A<>B and B<>C then "Scalene"

    end as triangles_type
    from TRIANGLES;

最佳答案

SELECT
  CASE 
    WHEN A + B <= C or A + C <= B or B + C <= A THEN 'Not A Triangle'
    WHEN A = B and B = C THEN 'Equilateral'
    WHEN A = B or A = C or B = C THEN 'Isosceles'
    WHEN A <> B and B <> C THEN 'Scalene'
  END tuple
FROM TRIANGLES;
  1. 通过使用 case 语句,检查给定的输入是否为三角形。
  2. 如果是三角形,则检查所有边是否相同。如果 true 三角形类型为“等边”。
  3. 如果不是,则检查是否有任何两条边相等。如果 true,则三角形类型为“等腰”
  4. 在不相等的情况下,三角形类型的任何边都是'Scalene'。我们也可以直接使用ELSE

关于mysql - MySQL中的三角形类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38561938/

相关文章:

mysql - 违反完整性约束 Magneto v1.9 向用户添加 REST 角色

mysql - 如何将多个事务表合并为一个查询,每个表代表一个状态?

MySQL:Digits (1.75) 到 Friendly Time(1 小时 45 分钟)

php - 即使在 PDO 准备语句和绑定(bind)参数之后也无法转义数组内的单引号

MYSQL "Column count doesn' t 匹配值计数在第 1 行"

javascript - Firefox Addon MySql 连接

mysql - 为什么 CodeIgniter Active Record 和 phpMyAdmin 给出不同的结果?

php - 存储用作散列密码一部分的散列函数是不好的做法吗?

php - SQL 错误。 SESSION 不能从 SELECT 获得主键

php - 从 PHP MySQL 连接获取 JSON