我想用PHP创建一个masndance系统的日期安排
所以我有:
表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
生成每个星期一直到x周。
问题是:如果我从DAY
编辑列SCHEDULE
,我该如何更新日期列?
即:我更新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 :(得分:0)
可能的解决方案是(假设'日期'时间'字段是varchar类型)使用STR_TO_DATE
和DATE_FORMAT
函数。然后,在以下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-> 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;
更改fri-> 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;
SQL FIDDLE DEMO:http://sqlfiddle.com/#!2/6b7221/2
注意:如果date
和time
字段的类型定义为Date
和Time
,则这些查询会更简单...然后,脚本只会是:
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;
答案 1 :(得分:0)
这可以通过触发器来完成,如果这一天总是前进的一天。如果Mon改为Fri,则会议日期将更改为下周五,依此类推。
为此,我将创建一个表来存储日期名称和数值值
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)