最佳的 MySQL 存储每周事件的方法?
让我们看看在 MySQL 中存储每周事件的最佳方法。首先创建一个新表格并包括每天的字段。
mysql> create table WeeklyEventDemo
-> (
-> ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> EventName varchar(20),
-> Monday tinyint(1),
-> Tuesday tinyint(1),
-> Wednesday tinyint(1),
-> Thursday tinyint(1),
-> Friday tinyint(1),
-> Saturday tinyint(1),
-> Sunday tinyint(1),
-> StartDate date,
-> EndDate date
-> );
Query OK, 0 rows affected (0.55 sec)
使用 insert 命令向表格中插入一些记录。查询如下所示−
mysql> insert into WeeklyEventDemo(EventName,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,StartDate,EndDate) values('Event-1',0,0,0,0,1,0,0,'2018-03-21','2019-03-08');
Query OK, 1 row affected (0.14 sec)
mysql> insert into WeeklyEventDemo(EventName,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday,StartDate,EndDate) values('Event-2',0,0,0,0,0,1,0,'2018-03-21','2019-03-08');
Query OK, 1 row affected (0.16 sec)
使用 select 语句显示表格中的所有记录。查询如下所示−
mysql> select * from WeeklyEventDemo;
以下是输出结果
+----+-----------+--------+---------+-----------+----------+--------+----------+--------+------------+------------+
| ID | EventName | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | StartDate | EndDate |
+----+-----------+--------+---------+-----------+----------+--------+----------+--------+------------+------------+
| 1 | Event-1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 2018-03-21 | 2019-03-08 |
| 2 | Event-2 | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 2018-03-21 | 2019-03-08 |
+----+-----------+--------+---------+-----------+----------+--------+----------+--------+------------+------------+
2 rows in set (0.00 sec)
以下是获取 MySQL 中每周事件的查询
mysql> select * from WeeklyEventDemo where Friday=1;
以下是输出结果
+----+-----------+--------+---------+-----------+----------+--------+----------+--------+------------+------------+
| ID | EventName | Monday | Tuesday | Wednesday | Thursday | Friday | Saturday | Sunday | StartDate | EndDate |
+----+-----------+--------+---------+-----------+----------+--------+----------+--------+------------+------------+
| 1 | Event-1 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 2018-03-21 | 2019-03-08 |
+----+-----------+--------+---------+-----------+----------+--------+----------+--------+------------+------------+
1 row in set (0.00 sec)
阅读更多:MySQL 教程