gpt4 book ai didi

Mysql Select 带有 case 语句和字段比较

转载 作者:行者123 更新时间:2023-11-29 21:08:38 25 4
gpt4 key购买 nike

我正在从具有字段比较值的表中进行选择,例如

Select FieldA,FieldB,(FieldC-FIeldD) as Difference

现在想要实际选择差异字段的值,例如

When Difference=0 then 'North'
When Different>0 and Difference<=90 then 'NorthEast'
When Difference=90 then 'South'

等等,但我无法将其插入到选择中。我已经在更新中成功完成了此操作,但“选择”却逃脱了我

最佳答案

我认为子选择查询在这里效果很好。

目前您的声明如下:

Select FieldA,FieldB,(FieldC-FIeldD) as Difference from <TABLE>

相反,您可以将其放在子选择中,如下所示:

select * from 
(
Select FieldA,FieldB,(FieldC-FIeldD) as Difference from <TABLE>
) sub_table

现在您可以在最外面的 select 语句中执行 case 语句:

select 
CASE
When Difference=0 then 'North'
When Difference>0 and Difference<=90 then 'NorthEast'
When Difference=90 then 'South'
END as answer
from
(
Select FieldA,FieldB,(FieldC-FIeldD) as Difference from <TABLE>
)

关于Mysql Select 带有 case 语句和字段比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36603325/

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