我在使用MySQL 5.5 / 5.6时遇到了一个奇怪的问题。 我在SQL中实现了一个经典的合并区间算法,但它并没有像预期的那样在某些行上运行。我使用Merging intervals in one pass in SQL和Most efficient way to sum overlapping datetimes in MySQL作为起始参考(C ++ @ http://www.geeksforgeeks.org/merging-intervals/)
任何人都能发现任何问题吗?
SELECT MIN(inizio) as inizio, MAX(fine) as fine
FROM (
SELECT
@interval_id := IF(attendances.inizio > @interval_end, @interval_id + 1, @interval_id) AS interval_id,
@interval_end := IF(attendances.inizio < @interval_end, GREATEST(@interval_end, attendances.fine), attendances.fine) AS interval_end,
attendances.inizio,
attendances.fine
FROM attendances
INNER JOIN attendance_sheets ON ( attendance_sheet_id = attendance_sheets.id)
ORDER BY attendances.inizio,attendances.fine
) intervals GROUP BY interval_id;
从这些行
我跳过了@interval_id而错误的@interval_end
导致2017-01-26错误合并
提前谢谢 尼古拉