我正在尝试构建自己的日历,并且我有一个用于输入事件的表单,如下所示。
我对如何设计 MySQL 表以及如何以可以轻松提取的方式记录这些信息感到困惑?
以下是我的尝试,它有效,但真的很丑,我相信它可以改进:
CREATE TABLE events (
event_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
title VARCHAR(100) NOT NULL,
location VARCHAR(100) NOT NULL,
body TEXT NOT NULL,
start_ts DATETIME NOT NULL,
end_ts DATETIME NOT NULL,
valid ENUM('Y','N') DEFAULT 'Y',
reoccurring ENUM('Y','N') DEFAULT 'N',
every ENUM('day','week','month','year',''),
bymonth ENUM('day','weekday',''),
end_date DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (event_id)
);
CREATE TABLE events_byweek (
event_id INTEGER UNSIGNED NOT NULL,
weekday TINYINT UNSIGNED NOT NULL,
FOREIGN KEY (event_id)
REFERENCES events(event_id)
);
-- returns all dates, reoccurring or otherwise within specified time range
-- by month
SELECT * FROM events WHERE (:year = YEAR(start_ts) AND :month = MONTH(start_ts))
OR (reoccurring = 'Y' AND ((YEAR(end_date) >= :year AND MONTH(end_date) >= :month) OR end_date = '0000-00-00 00:00:00')
AND (every != 'year' OR MONTH(start_ts) = :month)) AND valid = 'Y'
-- by day
SELECT * FROM events WHERE DATE(start_ts) = :date
OR (reoccurring = 'Y' AND (DATE(end_date) >= :date OR end_date = '0000-00-00 00:00:00')
AND (every != 'year' OR MONTH(start_ts) = :month)) AND valid = 'Y'
-- by week
SELECT * FROM events_byweek WHERE event_id = :event_id
我真的很感激任何建议,拜托!