我有一个脚本,它处理一些数据,然后将其导入mysql数据库中。
然后,我还有另一个脚本,它处理一些其他数据,然后将它导入mysql数据库中。
我想做的事:
我希望使用第三个脚本来控制(例如,查看上次导入了多少行),只控制与最后一个脚本一起导入的行。不是第一个导入的行(但我不希望删除第一个导入的行,只有最后一个导入的行)。
在MySQL或PHP中是否有这样的方法?
如果我不清楚什么事告诉我。
提前谢谢你!
发布于 2012-09-04 07:14:08
您可以始终向数据库中添加last_import列。每次脚本向mysql数据库导入某些内容之前,它都会将last_import的所有值更改为no或类似的值,然后输入其行的值为yes或类似的东西。使用此方法,您可以很容易地分辨出最后一次导入的是哪些。
假设这是你的数据库:
-----------------
| id | name |
-----------------
| 1 | Simon |
-----------------
| 2 | Richard|
-----------------
| 3 | Jon |
-----------------添加此字段:
-------------------------------
| id | name | last_import |
-------------------------------
| 1 | Simon | N |
-------------------------------
| 2 | Richard| N |
-------------------------------
| 3 | Jon | N |
-------------------------------因此,如果您正在使用mysql (如果不是,请告诉我),每次插入和处理数据时都这样做:
// Process data before here
$query = "UPDATE thetable SET last_import = 'N'"; // Changes all values of last_import in table to N
$result = @mysql_query($query) or die (mysql_error());
$query = "INSERT command here"; // do your inserting, while inserting the rows, put Y as the value of the last_import.然后在最后的检查文件中:
$query = "SELECT * FROM thetable WHERE last_import = 'Y'"; // Selects all that was last imported
// Process the query here那应该管用。
更新:感谢加文的建议(谢谢加文!)我还建议使用时间戳值。这样做是这样的:
你的桌子会是这样的
-------------------------------
| id | name | last_import |
-------------------------------
| 1 | Simon | 1346748315 |
-------------------------------
| 2 | Richard| 1346748315 |
-------------------------------
| 3 | Jon | 1346748315 |
-------------------------------在插入查询中:
// Process data before here
$currenttimestamp = time();
$query = "INSERT command here"; // do your inserting, while inserting the rows, put $currenttimestamp as the value of the last_import.在选择时:
$query = "SELECT last_import FROM thetable ORDER BY last_import DESC LIMIT 1"; // Selects the most recent timestamp
$result = @mysql_query($query) or die (mysql_error());
$mostrecenttstmp = @mysql_result($result, 0);
$query = "SELECT * FROM thetable WHERE last_import = '$mostrecenttstmp'";
// Process the query herehttps://stackoverflow.com/questions/12258423
复制相似问题