sql简单case语句

wgmfuz8q  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(308)

这个简单的sql语句似乎返回了错误的答案。有人能告诉我哪里出了问题吗。我正在使用mysql工作台。
我使用以下命令创建并填充表:

drop table if exists TRIANGLES;
create table TRIANGLES(A int, B int, C int);
insert into TRIANGLES values(20,20,23);
insert into TRIANGLES values(20,20,20);
insert into TRIANGLES values(20,21,22);
insert into TRIANGLES values(13,14,30);

我执行三角形类型的查询,如下所示:

select (case
when A+B<=C then 'Not a Triangle'
when B+C<=A then 'Not a Triangle'
when A+C<=B then 'Not a Triangle'
when A=B=C then 'Equilateral'
when A=B and B!=C then 'Isoscelus'
when B=C and C!=A then 'Isoscelus'
when A=C and B!=C then 'Isoscelus'
when A!=B!=C then 'Scalene'
end) as typ from TRIANGLES;

但我得到的答案是:

Isoscelus
Scalene -- bad result
Scalene
Not a Triangle

谢谢。

ni65a41a

ni65a41a1#

而不是 A=B=C 使用 A=B and B=C :

select *, (case
when A+B<=C then 'Not a Triangle'
when B+C<=A then 'Not a Triangle'
when A+C<=B then 'Not a Triangle'
when A=B and b=C then 'Equilateral'
when A=B and B!=C then 'Isoscelus'
when B=C and C!=A then 'Isoscelus'
when A=C and B!=C then 'Isoscelus'
when A!=B and B!=C and A!=C then 'Scalene' -- or just use: ELSE 'Scalene'
end) as typ 
from TRIANGLES;

结果:

A            B            C            typ             
-------------------------------------------------------
20           20           23           Isoscelus       
20           20           20           Equilateral     
20           21           22           Scalene         
13           14           30           Not a Triangle

相关问题