如何创建有条件的虚拟列
我有一个包含列 a
b
c
c 应该是带有条件的虚拟 =>
1- if 'b' < 16.5 the value should be "1"
2- if 'b' BETWEEN 16.5 AND 18.5 the value should be "2"
3- if 'b' = 25 the value should be "3"
最佳答案
您可以在以下情况下使用案例
select a, b, case when b <16.5 then 1
when b between 16.5 and 18.5 then 2
when b = 25 then 3
end c
from my_table
您还应该使用 else 来管理其他情况
select a, b, case when b <16.5 then 1
when b between 16.5 and 18.5 then 2
when b = 25 then 3
else 0
end c
from my_table
关于mysql - 如何在 MYSQL 中使用 if 查询创建虚拟列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58671765/