如何在多个记录中选择事件作为结果集中的天数
示例:
id | event | dateFrom | dateTo
1 festival 2012-06-15 01:18:00 2012-06-17 01:18:00
结果:
id | date
1 2012-06-15
1 2012-06-16
1 2012-06-17
答案 0 :(得分:1)
你做不到。最好的方法是制作一个简单的表格,填满一年的所有日期。 我做了一个简单的存储过程:
CREATE DEFINER = 'root_extern'@'%' PROCEDURE fill_calendar
(
IN start_date date,
IN end_date date
)
BEGIN
DECLARE crt_date DATE;
SET crt_date=start_date;
WHILE crt_date < end_date DO
INSERT INTO `calendar` (`date`) VALUES (crt_date);
SET crt_date = ADDDATE(crt_date, INTERVAL 1 DAY);
END WHILE;
END|
您的查询将如下所示:
SELECT date FROM calendar WHERE date BETWEEN dateFrom AND dateTo
答案 1 :(得分:0)
使用这样的联盟:
(SELECT id, dateFrom date FROM table) UNION (SELECT id, dateTo date FROM table)