gpt4 book ai didi

mysql - 有没有办法使用 MYSQL 中的 max() 函数创建多列?

转载 作者:行者123 更新时间:2023-11-29 20:18:15 25 4
gpt4 key购买 nike

我正在尝试使用以下公式从子表创建一个新表:

create table if not exists `new_table` as
select * from (
select descrip,
max(case when ID = 1.01 then value else 0 end) 1.01
from(
select ID, `JUL-08` value, 1 descrip
from original_table
union all
select ID, `AGO-08` value, 2 descrip
from original_table
union all
select ID, `SET-08` value, 3 descrip
from original_table
union all
select ID, `OUT-08` value, 4 descrip
from original_table
union all
select ID, `NOV-08` value, 5 descrip
from original_table
union all
select ID, `DEZ-08` value, 6 descrip
from original_table
) src
group by descrip
) as `new_table`;

该公式运行良好,它创建了要创建的表,但我想知道 max 函数是否可用于为同一个表创建多于 1 列,或者我是否必须为每个重复整个公式ID 可以创建一个新表,或者在同一公式中重复 max() 函数。

最佳答案

您可以根据需要多次重复表达式。您需要给它们指定不同的名称。

我还注意到您的表达式中有太多子查询:

create table if not exists `new_table` as
select descrip,
max(case when ID = 1.01 then value else 0 end) as `1.01`,
max(case when ID = 2.01 then value else 0 end) as `2.01`
from (select ID, `JUL-08` as value, 1 as descrip
from original_table
union all
select ID, `AGO-08` as value, 2 as descrip
from original_table
union all
select ID, `SET-08` as value, 3 as descrip
from original_table
union all
select ID, `OUT-08` as value, 4 as descrip
from original_table
union all
select ID, `NOV-08` as value, 5 as descrip
from original_table
union all
select ID, `DEZ-08` as value, 6 as descrip
from original_table
) src
group by descrip;

我不建议您将列命名为数字(或 SQL 关键字)。但如果你这样做,你应该使用转义字符来清楚你在做什么。我建议使用类似于 id_1_01 的内容,这样列名称就不需要转义。

关于mysql - 有没有办法使用 MYSQL 中的 max() 函数创建多列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39618509/

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