DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table (event_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,start_date DATE,end_date DATE);
INSERT INTO my_table VALUES (1,'2014-10-25','2014-11-10'),(2,'2014-11-01','2014-11-25');
SELECT * FROM my_table;
+----------+------------+------------+
| event_id | start_date | end_date |
+----------+------------+------------+
| 1 | 2014-10-25 | 2014-11-10 |
| 2 | 2014-11-01 | 2014-11-25 |
+----------+------------+------------+
SELECT GREATEST(x.start_date,y.start_date) start
, LEAST(x.end_date,y.end_date) end
, DATEDIFF(LEAST(x.end_date,y.end_date),GREATEST(x.start_date,y.start_date)) diff
FROM my_table x
JOIN my_table y
ON y.event_id > x.event_id
AND y.start_date < x.end_date
AND y.end_date > x.start_date;
+------------+------------+------+
| start | end | diff |
+------------+------------+------+
| 2014-11-01 | 2014-11-10 | 9 |
+------------+------------+------+
...或類似的東西。
你可以分享你的表格結構嗎? – Mureinik 2014-11-14 14:12:42