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)
삽입 명령을 사용하여 테이블에 일부 레코드를 삽입하십시오. 쿼리는 다음과 같습니다 -
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)