2

PHP 和 mySQL 问题我希望你能帮助我。我有一个更新数据库选项(通过单击一个按钮,它运行 update.php 文件),现在这个 update.php 负责尝试分离我的 PASSENGER 表。

目前,我有一个巨大的 PASSENGER 表,如果今天发生业务旅行(它是一家旅行预订公司),那么他们在 CUSTOMER 表中,但我想要任何存档的客户(例如从昨天旅行的任何人向后,放入 ARCHIVED_CUSTOMER 表中。

现在,我有一个小的 PHP 脚本,它使用当前 PASSENGER 表中的信息添加到 ARCHIVED_PASSENGER 表中,但仅限于参数是 PASSENGER ID 的地方。

我对脚本的 DELETE FROM PASSENGER 部分感到非常满意,但想知道是否有人知道允许我从数据库中收集和移动大量信息的代码,其中旅行日期是昨天起等等。它完成了通过 PASSENGER 表的 activeUntil 部分。

非常精简的 PHP 脚本,但希望我已经说清楚了!

   mysql_connect("localhost", "mysql_username", "mysql_password") or die(mysql_error());
    mysql_select_db("mysql_database") or die(mysql_error());

    $query10 = ("SELECT p.surname, p.passNo, p.activeUntil FROM PASSENGER p WHERE passNo = '20120003'");

            $result = mysql_query($query10);  
            while($row = mysql_fetch_array($result))
            {

$surname =  $row['surname'];
$passNo = $row['passNo']; 

mysql_query("INSERT INTO ARCHIVED_PASSENGER (surname, passNo) VALUES ('$surname', '$passNo')  ") 
    or die(mysql_error());  

    mysql_query("DELETE FROM PASSENGER (surname, passNo) VALUES ('$surname', '$passNo')  ") 
    or die(mysql_error());  


        }
4

2 回答 2

4

NOW() in MySQL gives the current timestamp. 24 hours ago is DATE_ADD(NOW(),INTERVAL -1 DAY).

You can compare dates just like numbers: activeUntil < DATE_ADD(NOW(),INTERVAL -1 DAY) selects rows where the activeUntil was anything before 24 hours ago.

You can combine INSERT and SELECT like so:

INSERT INTO `archived_passenger` (columns.....) SELECT columns... FROM `passenger` WHERE ...

(You can omit the first set of columns if the names of the columns in achived_passenger exactly match those in passenger, and you can use * instead of the second set of columns if you want to select all columns)

I think that should be enough infomation to help you do what you're trying to do. Let me know if you need any additional help.

于 2012-04-23T14:03:22.920 回答
1

假设您的“旅行日期”字段被调用activeUntil并且它是DATEorDATETIME数据类型:

... WHERE activeUntil >= DATE_ADD(NOW(), INTERVAL -1 DAY)

编辑:或者<=,取决于“从昨天开始”的意思。

于 2012-04-23T14:08:26.930 回答