MySQL查询时新建一列并根据条件赋值

MySQL查询时新建一列并根据条件赋值

原表是这样的

在这里插入图片描述

目标是:根据不同的机型赋值承载人数这一列,并按照每五分钟进行查询承载人数总和

首先试验一下根据条件赋值:case when (then else)

select 计划时间,机型,
case 
when '机型' = '波音73H' then 159
when '机型' = '空客320' then 152
when '机型' = '空客32B' then 256
when '机型' = '空客32V' then 211
when '机型' = '空客321' then 169
when '机型' = '空客319' then 179
else 122
end 承载人数
from sheet3
group by 计划时间

然后做一个虚拟的表查询

也就是根据目的,将添加的这个新列放到一张虚拟的表中,让我们在这个虚拟的表中按条件查询

select 计划时间,sum(承载人数)总和 from(
select *,
case 
when '机型' = '波音73H' then 159
when '机型' = '空客320' then 152
when '机型' = '空客32B' then 256
when '机型' = '空客32V' then 211
when '机型' = '空客321' then 169
when '机型' = '空客319' then 179
else 122
end as 承载人数
from sheet3
)
a group by 计划时间

结果如下

在这里插入图片描述



猜你喜欢

转载自blog.csdn.net/weixin_43697614/article/details/124219209
今日推荐