2014-10-17 59 views
2

我想用PHP創建附屬系統的日期計劃。
所以我有:
SCHEDULE
根據外鍵編輯計劃

id | room | day | class_id 
1 | 11 | Mon | 2 
3 | 12 | Tue | 3 
5 | 11 | Mon | 4 

MEETING

id | date  | time | schedule_id 
1 | 06 Oct 2014 | 9:00 | 1 
2 | 13 Oct 2014 | 9:00 | 1 
3 | 20 Oct 2014 | 9:00 | 1 
基於 SCHEDULE記錄生成

所以會議記錄,在這個例子中,MEETING id: 1,2,3基於SCHEDULE id: 1產生每星期一至星期。

所以問題是:如果我編輯從SCHEDULEDAY,如何更新日期列?
即:我更新SCHEDULE set day = "Fri" Where id = 1
所以表MEETING成爲

id | date  | time | schedule_id 
1 | 10 Oct 2014 | 9:00 | 1 
2 | 17 Oct 2014 | 9:00 | 1 
3 | 24 Oct 2014 | 9:00 | 1 

同時,我該如何處理,如果之前未經編輯的是星期五和更新,以週一?

回答

0

使用STR_TO_DATEDATE_FORMAT函數可能的解決方案是(假設'date'和'time'字段是varchar類型)。然後,在以下sql查詢上:

SET @schedule_id_to_modify = 1; 

UPDATE MEETING SET date =DATE_FORMAT(STR_TO_DATE(date,'%d %b %Y') 
            + INTERVAL 4 DAY,'%d %b %Y') 
WHERE schedule_id = @schedule_id_to_modify; 

UPDATE SCHEDULE SET day = DATE_FORMAT((
    SELECT STR_TO_DATE(date,'%d %b %Y') 
    FROM MEETING where schedule_id = @schedule_id_to_modify LIMIT 1) ,'%a') 
WHERE id = @schedule_id_to_modify; 

只是更改間隔和要修改的id。例如,

變化MON->週五:

SET @schedule_id_to_modify = 1; 

UPDATE MEETING SET date =DATE_FORMAT(STR_TO_DATE(date,'%d %b %Y') 
            + INTERVAL 4 DAY,'%d %b %Y') 
WHERE schedule_id = @schedule_id_to_modify; 

UPDATE SCHEDULE SET day = DATE_FORMAT((
    SELECT STR_TO_DATE(date,'%d %b %Y') 
    FROM MEETING where schedule_id = @schedule_id_to_modify LIMIT 1) ,'%a') 
WHERE id = @schedule_id_to_modify; 

變化fri->週一:

SET @schedule_id_to_modify = 1; 

UPDATE MEETING SET date =DATE_FORMAT(STR_TO_DATE(date,'%d %b %Y') 
            - INTERVAL 4 DAY,'%d %b %Y') 
WHERE schedule_id = @schedule_id_to_modify; 

UPDATE SCHEDULE SET day = DATE_FORMAT((
    SELECT STR_TO_DATE(date,'%d %b %Y') 
    FROM MEETING where schedule_id = @schedule_id_to_modify LIMIT 1) ,'%a') 
WHERE id = @schedule_id_to_modify; 

SQL FIDDLE DEMO:http://sqlfiddle.com/#!2/6b7221/2

注:這些查詢是,如果更加簡單datetime字段的類型被定義爲DateTime ......然後,腳本會只有:

SET @schedule_id_to_modify = 1; 

UPDATE MEETING SET date = date + INTERVAL 4 DAY 
WHERE schedule_id = @schedule_id_to_modify; 

UPDATE SCHEDULE SET day = DATE_FORMAT((
    SELECT date FROM MEETING where schedule_id = @schedule_id_to_modify 
    LIMIT 1) ,'%a') 
WHERE id = @schedule_id_to_modify; 
+0

感謝的人,這是exatctly我需要,其簡單易懂 – 2014-10-17 14:35:47

0

這可以通過觸發器完成,並且如果一天總是前進的一天。意思是如果星期一更改爲星期五,則會議日期將更改爲下週五,依此類推。

爲此,我將創建一個表來存儲當天名稱和數值的東西作爲

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)