为什么我通过在mySql中使用union来获取null作为输出?

时间:2017-11-03 05:20:45

标签: mysql

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.
Not A Triangle: The given values of A, B, and C don't form a triangle.

A,B和C是三角形表中的列名称

SQL查询

(
SELECT distinct 
CASE 
when (a=b and a<>c) 
or (b=c and a<>c) 
or (a=c and a<>b) 
then 'Isosceles' 
end 
from triangles
) 
union
(
SELECT distinct 
CASE 
when a=b 
and b=c 
then 'Equilateral' 
end 
from triangles
)
union 
(
SELECT distinct 
CASE 
when a!=b 
and b!=c 
and a!=c 
then 'Scalene' 
end 
from triangles
)
union
( 
SELECT distinct 
CASE 
when a+b<c 
or b+c <a 
or a+c<b 
then 'Not A Triangle' 
end 
from triangles
);

输出

NULL

Isosceles

等边

Scalene

不是三角形

1 个答案:

答案 0 :(得分:0)

您可以尝试:

SELECT distinct 
CASE 
when (a=b and a<>c) 
or (b=c and a<>c) 
or (a=c and a<>b) 
then 'Isosceles' 
when a=b 
and b=c 
then 'Equilateral' 
when a!=b 
and b!=c 
and a!=c 
then 'Scalene' 
else 'Not A Triangle' 
end 
from triangles

您可以根据需要更改条件。