這可以通過觸發器完成,並且如果一天總是前進的一天。意思是如果星期一更改爲星期五,則會議日期將更改爲下週五,依此類推。
爲此,我將創建一個表來存儲當天名稱和數值的東西作爲
mysql> create table days (day varchar(10),day_number int);
Query OK, 0 rows affected (0.07 sec)
mysql> insert into days values ('Sun',0),('Mon',1),('Tue',2),('Wed',3),('Thu',4),('Fri',5),('Sat',6);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
下一步將創建下列觸發更新之後,將觸發日程
DELIMITER //
create trigger update_schedule after update on schedule
for each row
begin
declare existing_day varchar(10);
declare updated_day varchar(10);
declare diff_day int;
if new.day <> old.day then
select day_number into existing_day from days where LOWER(day) = LOWER(old.day);
select day_number into updated_day from days where LOWER(day) = LOWER(new.day);
set diff_day = updated_day - existing_day ;
if diff_day < 0 then
set diff_day = diff_day * -1 ;
end if ;
update meeting set date = DATE_ADD(date , interval diff_day day) where schedule_id = new.id;
end if;
end; //
delimiter ;
現在讓我們做一些測試
mysql> create table schedule (id int, room int, day varchar(100),class_id int);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into schedule values (1,11,'Mon',2),(2,12,'Tue',3),(5,11,'Mon',4);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> create table meeting (id int, date date, time time,schedule_id int);
Query OK, 0 rows affected (0.08 sec)
mysql> insert into meeting values (1,'2014-10-06','9:00',1),(2,'2014-10-13','9:00',1),(3,'2014-10-20','9:00',1);
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0
現在讓我們更新計劃
mysql> update schedule set day = 'Fri' where id = 1 ;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
現在,如果我們選擇會議的數據,我們應該可以看到如下的變化 -
mysql> select * from meeting ;
+------+------------+----------+-------------+
| id | date | time | schedule_id |
+------+------------+----------+-------------+
| 1 | 2014-10-10 | 09:00:00 | 1 |
| 2 | 2014-10-17 | 09:00:00 | 1 |
| 3 | 2014-10-24 | 09:00:00 | 1 |
+------+------------+----------+-------------+
3 rows in set (0.00 sec)
感謝的人,這是exatctly我需要,其簡單易懂 – 2014-10-17 14:35:47