在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
   -> );

使用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');
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');

使用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)