gpt4 book ai didi

mysql - SQL 简单案例语句

转载 作者:行者123 更新时间:2023-11-29 02:40:58 26 4
gpt4 key购买 nike

这个简单的 SQL 语句似乎返回了错误的答案。有人可以告诉我哪里出错了。我正在使用 MySQL Workbench。

我使用以下命令创建并填充表:

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

谢谢。

最佳答案

使用 A=B 和 B=C 代替 A=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

关于mysql - SQL 简单案例语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52240819/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com