[英]MYSQL Select case when query returns NULL for output
Running a nested case when query to classify entries in a table based on the parameters below. 查询时运行嵌套案例,以根据以下参数对表中的条目进行分类。 When I run the code, the output for
Isosceles
returns Null
. 当我运行代码时,
Isosceles
的输出返回Null
。 I'm not sure why. 我不知道为什么。 I'm comparing integers a,b, and c, which represent columns for each of those distinct integers for a triangle, for instance.
我正在比较整数a,b和c,例如,它们代表三角形中每个不同整数的列。
Any suggestions? 有什么建议么?
select case when a+b > c and b+c > a and c+a > b then
case
when a=b|a=c|b=c then "Isosceles"
when a=b and a=c and b=c then "Equilateral"
when a<>b and b<>c and a<>c then "Scalene"
end
else "Not A Triangle"
end
from triangles
First problem: Instead of single pipe, use double pipe ||
第一个问题:相反的单管,采用双管
||
for OR
. 对于
OR
。
Second problem: Use Equilateral
condition before Isosceles
So if your sides are 2
, 2
and 2
then as per your logic, Isosceles
condition satisfies first. 问题二:使用
Equilateral
前条件Isosceles
所以,如果你的侧面2
, 2
和2
,然后按你的逻辑, Isosceles
条件满足第一。 So it will never equate it to Equilateral
所以它永远不会等同于
Equilateral
New Answer: In fact, you don't need 2 case statements. 新答案:实际上,您不需要2个case语句。 Use this instead
改用这个
http://rextester.com/PTDI48631 http://rextester.com/PTDI48631
select t.*,
case when not (a+b > c and b+c > a and c+a > b)
then "Not A Triangle"
when (a=b and a=c and b=c)
then "Equilateral"
when (a=b or a=c or b=c)
then "Isosceles"
else "Scalene"
end as Triangle_type
from triangle t;
Previous Answer: http://rextester.com/ELLB75736 上一个答案: http : //rextester.com/ELLB75736
select t.*,
case when a+b > c and b+c > a and c+a > b
then
(case
when a=b and a=c and b=c then "Equilateral"
when a=b or a=c or b=c then "Isosceles"
when a<>b and b<>c and a<>c then "Scalene"
end
)
else
"Not A Triangle"
end as Triangle_type
from triangle234 t
select case when a+b > c and b+c > a and c+a > b then
case
when (a=b & a=c)|(a=b & b=c) | (a=c & b=c) then "Isosceles"
when a=b and a=c and b=c then "Equilateral"
when a<>b and b<>c and a<>c then "Scalene"
Else "Triangle"
end
else "Not A Triangle"
end
from triangles
Just try above code.I think you missed else
for internal WHEN
. 刚刚尝试上面code.I想你错过
else
内部WHEN
。 Hope this will helps. 希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.