我试图解决检测三角形类型的Hackerrank问题,我有以下代码:
SELECT A,B,C FROM TRIANGLES
CASE
WHEN (A >= B + C) OR (B >= A + C) OR (C >= A + B) THEN "Not A Triangle"
WHEN A = B AND B = C THEN "Equilateral"
WHEN (A = B AND B = C) OR (B = C AND C = A) OR (C = A AND A = B) THEN "Isosceles"
ELSE "Scalene"
END;
但它不起作用:
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CASE
WHEN (A >= B + C) OR (B >= A + C) OR (C >= A + B) THEN "Not A Trian' at line 2
答案 0 :(得分:2)
我想你想要你的输出:
A B C TYPE
6 2 3 Not a Triangle
3 3 3 Equilater
依旧......
试试这个:
SELECT A,B,C,
CASE
WHEN (A >= B + C) OR (B >= A + C) OR (C >= A + B) THEN "Not A Triangle"
WHEN A = B AND B = C THEN "Equilateral"
WHEN (A = B AND B = C) OR (B = C AND C = A) OR (C = A AND A = B) THEN "Isosceles"
ELSE "Scalene"
END AS type
FROM TRIANGLES
答案 1 :(得分:0)
不确定您打算做什么,但这应该有效:
SELECT A,B,C ,
CASE
WHEN (A >= B + C) OR (B >= A + C) OR (C >= A + B) THEN 'Not A Triangle'
WHEN A = B AND B = C THEN 'Equilateral'
WHEN (A = B AND B = C) OR (B = C AND C = A) OR (C = A AND A = B) THEN 'Isosceles'
ELSE 'Scalene'
END
FROM TRIANGLES
你有两个错误,首先,字符串需要用单引号包装,双引号用于列名。其次,语法是:
SELECT <columns>
FROM <Table>
WHERE <Filters>