我需要做一个老师的日程表
数据库中有一个table(teacher)是所有老师列表,大概是这样:
id name
1  teacher1
2  teacher2
还有一个table(teacherCalendar)是日期,每个老师对应的至少有7天,大概如下:
id    teacherId     date
1     1             2009-1-21
2     1             2009-1-22
...   ...           ...
      2             2009-1-22我现在想的是,是否可以在每天的某个时间点,在teacherCalendar表中,增加记录,记录的内容是每个老师的一天时间现在不知道怎么实现,如何定时实现

解决方案 »

  1.   

    可以
    在你的MySQL所在的主机上定时执行一个脚本,比如下面这个命令。[code=BatchFile]mysql -u userid -ppasswd mydb -e 'insert into teacherCalendar(teacherId,date) select id,CURDATE() from teacher;'[/code]这样每天可以把所有教师在表teacherCalendar添加当日的记录,你可以在主机定在早上5点运行.Linux下见参见crontab命令, windows下则简单,直接看计划任务就行了
      

  2.   

    主机是虚拟主机,我可能不能在上面定时执行脚本。我看了下mysql的事件(Event),可以定时执行SQL语句,但是我这里执行的东西有点复杂,我不知道怎么写。就是要判断teacherCalender里面对应某个老师,date列从当天算起往后记录如果少于7天就把它补到7天,比如:今天是2009-1-20日,如果只有2009-1-21这一条记录,那么就要增加6条记录,分别是2009-1-22,...,2009-1-27。这些弄好了以后,要在另外一个table(teacher_calendar_time)里面,要插入对应每一天的24条记录,是按小时的,如:2009-1-21这天,就有:
    2009-1-21 00:00-01:00
    2009-1-21 01:00-02:00
    ...       ...
    2009-1-21 23:00-00:00
    这样的24条记录请教如何写
    谢谢
      

  3.   

    中间的语句自己写吧。不过周围还真没人用过这个功能。DELIMITER |CREATE EVENT e_daily
        ON SCHEDULE
          EVERY 1 DAY
        COMMENT 'test..'
        DO
          BEGIN
            INSERT INTO site_activity.totals (when, total)
              SELECT CURRENT_TIMESTAMP, COUNT(*) 
                FROM site_activity.sessions;
            DELETE FROM site_activity.sessions;
          END |DELIMITER ;
      

  4.   

    -----------------------
    我这里执行的东西有点复杂,我不知道怎么写。就是要判断teacherCalender里面对应某个老师,date列从当天算起往后记录如果少于7天就把它补到7天,比如:今天是2009-1-20日,如果只有2009-1-21这一条记录,那么就要增加6条记录,分别是2009-1-22,...,2009-1-27。这些弄好了以后,要在另外一个table(teacher_calendar_time)里面,要插入对应每一天的24条记录,是按小时的,如:2009-1-21这天,就有: 
    2009-1-21 00:00-01:00 
    2009-1-21 01:00-02:00 
    ...      ... 
    2009-1-21 23:00-00:00 
    这样的24条记录 
    --------------------------
    我的主要问题是 上面这些处理我不知道怎么写SQL,谢谢各位,望不吝赐教
      

  5.   

    不想去搭环境去写个代码了。可以提供一个简单算法。declare @bdate DATE;
    declare i int;
    declare j int;
    set @bdate=curdate();
    set i=0;while i<8
    insert into teacherCalendar(teacherId,date) select id,@bdate from teacher where id not in (select id from teacherCalendar where date=@bdate);
    insert into teacher_calendar_time(teacherId,date,xxxCol) select id,@bdate from teacher  where not exists (select id from teacher_calendar_time where date=@bdate and teacherId=teacher.id and xxxCol='00:00-01:00');
    insert into teacher_calendar_time(teacherId,date,xxxCol) select id,@bdate from teacher  where not exists (select id from teacher_calendar_time where date=@bdate and teacherId=teacher.id and xxxCol='01:00-02:00');
    ....
    insert into teacher_calendar_time(teacherId,date,xxxCol) select id,@bdate from teacher  where not exists (select id from teacher_calendar_time where date=@bdate and teacherId=teacher.id and xxxCol='23:00-00:00');
    set i=i+1;
    set @bdate=@bdate+1;
    end while;
      

  6.   

    ACMAIN_CHM 我用的是mysql,报@bdate DATE;
    declare i int;
    declare j int;
    set @bdate=curdate();
    set i=0;
    这附近有错,不知道哪错了,是@符号不要吗,如果不要,下面的insert语句里面该怎么写?
      

  7.   

    还有我在mysql里create了一个简单的event
    CREATE EVENT test
          ON SCHEDULE
            EVERY 5 SECOND
          DO
            BEGIN
               DECLARE i int;
               SET i=0;
               WHILE i<2 DO
                 insert into teacher(name) values('teacher');
                 SET i=i+1;
               END WHILE
            END为什么不执行呢,我发现teacher这个table里面的记录没变
      

  8.   

    先create 一个procdure 试试,以断定是 event的问题还是程序的问题。        BEGIN
               DECLARE i int;
               SET i=0;
               WHILE i<2 DO
                 insert into teacher(name) values('teacher');
                 SET i=i+1;
               END WHILE
            END
      

  9.   

    用procedure是对的,看来不是程序的问题event需要什么配置吗 
      

  10.   

    可以执行了 ^_^
    现在的问题是
    while i<8
        insert into teacherCalendar(teacherId,date) select id,@bdate from teacher where id not in (select id from teacherCalendar where date=@bdate);
        insert into teacher_calendar_time(teacherId,date,xxxCol) select id,@bdate from teacher     where not exists (select id from teacher_calendar_time where date=@bdate and teacherId=teacher.id and xxxCol='00:00-01:00');
        insert into teacher_calendar_time(teacherId,date,xxxCol) select id,@bdate from teacher     where not exists (select id from teacher_calendar_time where date=@bdate and teacherId=teacher.id and xxxCol='01:00-02:00');
        ....
        insert into teacher_calendar_time(teacherId,date,xxxCol) select id,@bdate from teacher     where not exists (select id from teacher_calendar_time where date=@bdate and teacherId=teacher.id and xxxCol='23:00-00:00');
        set i=i+1;
        set @bdate=@bdate+1;
    end while;这里面bdate自动加以,我测试的时候,他的值变成20090132的时候,就插入不成功了,因为20090132不是一个正确的日期。如何能让他自动变成20090101呢?