douguio0185 2012-05-15 10:30
浏览 81
已采纳

在特定条件下归档数据

I have two tables, projects and archive with identical columns. As you understand, I need to archive projects in the archive table, under certain conditions.

I wrote two requests, and they work fine if executed separately, but I'd like to assemble them.

$sql = "INSERT INTO archive (idDocument,documentNumber,submissionDate,projectName)
        SELECT idDocument,documentNumber,submissionDate,projectName
        FROM projects
        WHERE id='".$id."'";

$sql = "DELETE FROM projects
        WHERE id='".$id."'";

1/ First of all, how can I assemble these two requests? I keep getting syntax errors.

2/ Now, I assign projects a status (in a column named status) from "1" to "3" in PHP and I'd like to archive these projects two months after they have reached status "3". How can I do so?

Thank you very much for your help.

  • 写回答

1条回答 默认 最新

  • douwu8060 2012-05-15 10:53
    关注

    Do not know what You mean by assembling, maybe You want to query these two queries in just one simple call, then each query should be ended by query delimiter - a semicolon ;, like this:

    $sql = "INSERT INTO archive (idDocument,documentNumber,submissionDate,projectName)
        SELECT idDocument,documentNumber,submissionDate,projectName
        FROM projects
        WHERE id='".$id."';
    
        DELETE FROM projects
        WHERE id='".$id."';";
    

    If You want to archive+delete the projects with status 3 after two months they had been statused by 3, You will have to do these things:

    1. when updating the project to status 3 also set the date_changed = NOW() (or similar expression)
    2. write down a CRON job that will be executed let's say every day at 3:00 am.
    3. the CRON job will call a PHP script that will walk down the projects table and archive + delete all the projects with status 3 where date_changed is less then or equal to NOW() - INTERVAL '2' MONTHS...

    That should do the trick...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?