mysql - SQL 简单案例语句
问题描述
这个简单的 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
谢谢。
解决方案
而不是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
推荐阅读
- c# - 如何使用 GetAsyncEnumerator 中止正在运行的 EF Core 查询?
- abp - 在从 UI (ABP.IO) 创建租户的过程中创建新角色(通过代码)
- c# - How to display PointGeometryModel3D in GroupModel3D via Binding
- sql - 案例(如果有的话)
- vue.js - 如何在 b-table 模板标签中显示来自道具的数据?
- firebase - 如何允许所有用户仅阅读具有“朋友:真”字段的某些文档
- python - 我需要你的帮助来解决这个问题,看起来很容易,但我每次都会出错
- sql - Power BI DAX - 找到可重复性
- java - 如何使用带有 Jbutton 的 actionlistener 根据 JComboBox 中的选项绘制形状?
- regex - Perl:里面有变量的正则表达式?