MySQL MYSQL中的三角形类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38561938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Type of Triangle in MYSQL
提问by zathura
Problem statement:
问题陈述:
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.
编写一个查询,使用其三边长度标识 TRIANGLES 表中每条记录的类型。为表中的每条记录输出以下语句之一:
- 非三角形:A、B 和 C 的给定值不形成三角形。
- 等边:三角形的边长相等。
- 等腰:三角形的边长相等。
- Scalene:这是一个边长不同的三角形。输入格式
TRIANGLES 表描述如下:
表中的每一行表示三角形三边中每条边的长度。
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
Attempt that did not work:
尝试无效:
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;
回答by Anurag garg
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;
- By using case statement check if given input is a triangle or not.
- If it is a triangle then check if all sides are same if truethe triangle type is 'Equilateral'.
- If not then check if any two sides are equal if truethe triangle type is 'Isosceles'
- In the case of not equal, any sides the triangle type is 'Scalene'. We can directly use ELSEalso.
- 通过使用 case 语句检查给定的输入是否为三角形。
- 如果是三角形,则检查所有边是否相同,如果为真三角形类型为“等边”。
- 如果不是,则检查任何两条边是否相等,如果为真三角形类型为“等腰”
- 在不相等的情况下,三角形类型的任何边都是'Scalene'。我们也可以直接使用ELSE。
回答by zathura
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"
else "Scalene"
end as triangles_type
from TRIANGLES;
回答by DEVNAG BHARAD
SELECT CASE
WHEN A+B>C AND B+C>A AND A+C>B THEN
CASE
WHEN A=B AND B=C THEN 'Equilateral'
WHEN A=B or A=C OR B=C THEN 'Isosceles'
ELSE 'Scalene'
END
ELSE 'Not A Triangle'
END
FROM TRIANGLES
Step 1: First we select case to check if given input is a triangle or not
Step 2: If it is a triangle then check for its type. So first case if all sides are the same, then the triangle type is
'Equilateral'
, then second case if any two sides are the same then the triangle type is'Isosceles'
, otherwise the triangle type is'Scalene'
.
步骤 1:首先我们选择 case 来检查给定的输入是否是三角形
第 2 步:如果是三角形,则检查其类型。所以第一种情况如果所有边都相同,则三角形类型为
'Equilateral'
,然后第二种情况如果任何两条边相同则三角形类型为'Isosceles'
,否则三角形类型为'Scalene'
。
回答by Bartu_D
Your answer is actually right. But the one little information (Triangle's 'Right' Definition)is missing on the question (which is not your fault) that if two sides' sum is bigger OR EQUAL to another side then it's not a triangle
你的回答其实是对的。但是这个问题上缺少一个小信息(三角形的“正确”定义)(这不是您的错),如果两侧的总和大于或等于另一侧,则它不是三角形
The only thing you should do is putting '=' near of your '<'
你唯一应该做的就是把 '=' 放在你的 '<' 附近
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;
回答by kienonline19
SELECT
CASE
WHEN (A > 0 AND B > 0 AND C > 0 AND (A + B > C) AND (B + C > A) AND (A + C > B))
THEN
(
CASE
WHEN (A = B AND B = C) THEN 'Equilateral'
WHEN (A = B OR B = C OR A = C) THEN 'Isosceles'
ELSE 'Scalene'
END
)
ELSE 'Not A Triangle'
END
FROM TRIANGLES;
回答by Satyansh
select case
when (A+B<=C or B+C<=A or A+C<=B) then 'Not A Triangle'
when (A=B and B=c) then 'Equilateral'
when (A=B AND C<>B)or(B=C AND C<>A)or(A=C AND A<>B) then 'Isosceles'
else 'Scalene'
end as triangle_type
from TRIANGLES;
here, it will strictly check for the type of the triangles.
在这里,它将严格检查三角形的类型。
回答by Surya Pratap
For Oracle 11g this is working:
对于 Oracle 11g,这是有效的:
SELECT CASE WHEN A+B>C then case when A=B AND B=C THEN 'Equilateral'
WHEN A=B OR B=C OR A=C THEN 'Isosceles'
WHEN A!=B or B!=C OR A!=C THEN 'Scalene' END
ELSE 'Not A Triangle' END
FROM TRIANGLES;
回答by user7481739
SELECT CASE
WHEN (A + B <= C) OR (B+C <= A) OR (A+C <= B) THEN "Not A Triangle"
WHEN (A=B) AND (B=C) THEN "Equilateral"
WHEN (A=B) OR (C=A) OR (B=C) THEN "Isosceles"
ELSE "Scalene"
END
FROM TRIANGLES
回答by Varun
Give it a try. That should definitely work.
试一试。那应该肯定有效。
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'
ELSE 'Scalene'
END
FROM TRIANGLES;
回答by jishadaniel
SELECT
CASE
WHEN (A+B>C AND B+C>A AND A+C>B) THEN
(CASE
WHEN A=B AND B=C AND A=C THEN 'Equilateral'
WHEN A=B OR B=C OR A=C THEN 'Isosceles'
ELSE 'Scalene'
END)
ELSE 'Not A Triangle'
END
FROM TRIANGLES;