Summing range of dates without counting overlaps in mysql

前端 未结 3 1940
遇见更好的自我
遇见更好的自我 2021-01-26 18:09

I have simple table with start_date and end_date columns in it. These date values may overlap

id    start_date    end_date
1     2011-0         


        
3条回答
  •  遥遥无期
    2021-01-26 18:12

    If you need the total periods for a number of records including the overlaps, then simply sum the period differences per record:

    SELECT SUM(PERIOD_DIFF( DATE_FORMAT(end_date, '%Y%m') , DATE_FORMAT(start_date, '%Y%m') )) AS total_periods
    FROM table WHERE ...
    

提交回复
热议问题