简体   繁体   中英

save data in mysql by stored procedure

i have a table that must by update every month, due i upload text file on server and save it in temporary table, the bellow code show it:

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

then after update successfully and insert new data in "infemptem" table, i should compare the data of main table's "infemp" that have old data by "infemptemp" that have new data, for do this Scenario i write bellow code:

$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

by attention to the code and have 3500 record in "infempsame " table on my database, when i want update "infemp" table, the time out on browser occur. I think if write above code by procedure, my problem solved. Is it true? How do i can write procedure and call or use it? Best Regards.

A stored procedure would be overkill. There is a better way hidden away but it get's only a very brief mention in the 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;

Thus you can write a simple query to get rid of the PHP script like this.

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

if you have an index on the prsID column this should complete very quickly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM