将Oracle分层查询转换为MySQL

时间:2016-02-18 12:38:18

标签: mysql oracle

select to_char(
         (to_date(20160218, 'yyyymmdd') + (level - 1) / 24),
         'yyyymmddhh24'
       ) dt
from dual
connect by level <= 24

此SQL可以在Oracle中运行,但MySQL不支持这些分层函数(connect by level)。

如何将其翻译成MySQL?

1 个答案:

答案 0 :(得分:0)

您不一定需要递归查询才能生成24个值。您可以改为使用UNION ALL

select '2016021800' as dt
union all
select '2016021801' as dt
union all
...
union all
select '2016021823' as dt;

变量日期也是如此,例如当前日期:

select date_format(curdate(), '%Y%m%d') || '01'
union all
select date_format(curdate(), '%Y%m%d') || '02'
union all
...
union all
select date_format(curdate(), '%Y%m%d') || '23';