gpt4 book ai didi

php - 通过存储过程在mysql中保存数据

转载 作者:行者123 更新时间:2023-11-28 23:50:34 25 4
gpt4 key购买 nike

我有一个必须每月更新的表格,由于我在服务器上上传文本文件并将其保存在临时表中,下面的代码显示了它:

$result_emp = "LOAD DATA LOCAL INFILE'".$myFile_emp."' INTO TABLE infemptemp COLUMNS TERMINATED BY ','";

然后在成功更新并在“infemptem”表中插入新数据后,我应该将具有旧数据的主表“infemp”的数据与具有新数据的“infemptemp”的数据进行比较,为此场景我编写了以下代码:

$viwe_emp =mysqli_query($conn,"select * from infempsame where 1");
$NumRows_emp=mysqli_num_rows($viwe_emp);
//echo $NumRows_vam;
$i=0;

while(($row=$viwe_emp->fetch_assoc())!=NULL)
{
$prsid1=$row['PrsID'];
$AccID1=$row['AccID'];
$numid1=$row['NumID'];
$sql2="select * from infempsame where prsID='".$prsid1."';";
if (mysqli_query($conn,$sql2))
{
$sql2_update="UPDATE `infemp` SET AccID='".$AccID1."',NumID='".$numid1."' WHERE prsID='".$prsid1."';";
if(mysqli_query($conn,$sql2_update))
{
$i+=1;
}
}// end if (mysqli_query($conn,$sql2))

}// end while insert new fields of infvam

注意代码,在我的数据库的“infempsame”表中有 3500 条记录,当我想更新“infemp”表时,浏览器超时。我想如果按程序编写上面的代码,我的问题就解决了。是真的吗?我如何编写程序并调用或使用它?最好的问候。

最佳答案

存储过程会有点矫枉过正。隐藏了一个更好的方法,但它在 manual 中只是非常简短地提及。

You can also perform UPDATE operations covering multiple tables. However, you cannot use ORDER BY or LIMIT with a multiple-table UPDATE. The table_references clause lists the tables involved in the join. Its syntax is described in Section 13.2.9.2, “JOIN Syntax”. Here is an example:

UPDATE items,month SET items.price=month.price WHERE
items.id=month.id;

因此您可以编写一个简单的查询来摆脱这样的 PHP 脚本。

UPDATE infemp, infemptemp 
SET infemp.AccID=infemptemp.AccID,
infemp.NumID=infemptemp.numID
WHERE infemp.prsID=infemptemp.prsID;

如果您在 prsID 列上有索引,这应该会很快完成。

关于php - 通过存储过程在mysql中保存数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32684025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com