简体   繁体   中英

mysql select event as multiple records

how can i select event in multiple records as days in resultset

Example:

id     |     event    |     dateFrom            |       dateTo
1           festival     2012-06-15 01:18:00        2012-06-17 01:18:00

Result:

id    |  date
1       2012-06-15 
1       2012-06-16
1       2012-06-17  

You can't. Best way is to make a simple table filled with all the dates of a year. I made a simple stored procedure:

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|

Your query will look like: SELECT date FROM calendar WHERE date BETWEEN dateFrom AND dateTo

使用这样的联合:

(SELECT id, dateFrom date FROM table) UNION (SELECT id, dateTo date FROM table)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM