简体   繁体   中英

Translate Oracle Hierarchical Query to MySQL

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

This SQL can run in Oracle but MySQL does not support these hierarchical functions ( connect by level ).

How can it be translated to MySQL?

You don't necessarily need a recursive query in order to generate 24 values. You can use UNION ALL instead:

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

The same is also possible with a variable date, such as the current date:

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';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM