List of months within multiple date ranges in mysql -
i have list of date ranges , list of months within these date ranges. can query date ranges so:
select id, start, end date_range
and query give following output:
1, 01-01-2016, 25-03-2016 2, 26-03-2016, 30-03-2016 3, 30-12-2016, 08-01-2017
now find mysql query lists months within these date ranges. should give following output:
01-2016 02-2016 03-2016 12-2016 01-2017
there examples here on how list of month between 2 dates, such as:
but these examples single date range, have multiple date ranges. great if can find sql query problem.
here solution:
#drop table if exists monthtest; create table monthtest(id int unsigned auto_increment primary key, `start` datetime, `end`datetime); insert monthtest(`start`, `end`) values ('2016-01-01', '2016-03-25'), ('2016-03-26', '2016-03-30'), ('2016-12-30', '2017-08-01'); select a.`start`, a.`end`, date_format(date_add(a.`start`, interval b.help_keyword_id month), '%y%m') monthtest a, mysql.help_keyword b period_diff(date_format(a.`end`, '%y%m'), date_format(a.`start`, '%y%m')) >= b.help_keyword_id order a.id;
note query in second join
table has dependency table must contain more rows maximum number of months between 2 dates , join field must incrementing integer
starting 0. due limitation mysql doesn't (yet) contain row generator workaround necessary.
regards,
james
wiki
Comments
Post a Comment