When MySQL queries, create a new column and assign values according to conditions

When MySQL queries, create a new column and assign values ​​​​according to conditions

The original table is like this

insert image description here

The goal is: assign the column of the number of passengers according to different models, and query the total number of passengers every five minutes

First experiment with conditional assignment: 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 计划时间

Then do a dummy table query

That is, according to the purpose, put the added new column into a virtual table, let us query by condition in this virtual table

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 计划时间

The result is as follows

insert image description here



Guess you like

Origin blog.csdn.net/weixin_43697614/article/details/124219209