MySQL如何将列转换为行?
有一列表示24小时MySQL如何将列转换为行?,mysql,sql,Mysql,Sql,有一列表示24小时 数据每15分钟一次 例如,在10:15,该值被输入到10H列的DateTime列中。 但是我想要的, 我想通过结合时间列和日期时间来逐行显示时间和值 与2020-11-12年01时30分相同 世界其他地区:2020-11-12 00:30 柱:1H 将于2020年12月12日01:30发布 世界其他地区:2020-11-12 01:30 列:值 原样 将来 我想你想要联合所有人: select datetime, `0H` as value from t union
数据每15分钟一次
例如,在10:15,该值被输入到10H列的DateTime列中。
但是我想要的, 我想通过结合时间列和日期时间来逐行显示时间和值
- 与2020-11-12年01时30分相同
世界其他地区:2020-11-12 00:30
柱:1H - 将于2020年12月12日01:30发布
世界其他地区:2020-11-12 01:30
列:值
我想你想要联合所有人:
select datetime, `0H` as value
from t
union all
select datetime + interval 1 hour, `1H`
from t;
您也可以使用交叉连接来执行此操作:
select (datetime + interval h hour) as datetime,
(case when h.h = 0 then `0H` else `1H` end) as value
from t cross join
(select 0 as h union all select 1 as h) h
@草莓:我打赌你会很难找到Gordon Linoff没有回答的任何SQL问题……@GMB True,但在这个例子中,它是一样的author@Strawberry:是的,我知道了。但问题并不完全相同,答案也是如此。我不止一次看到Gordon把完全相同的问题作为重复的问题来结束(当我发现这种模式时,我也倾向于这样做)。
select datetime, `0H` as value
from t
union all
select datetime + interval 1 hour, `1H`
from t;
select (datetime + interval h hour) as datetime,
(case when h.h = 0 then `0H` else `1H` end) as value
from t cross join
(select 0 as h union all select 1 as h) h