[英]SQL and PHP Batch Update
I have the following table with the following values : 我有具有以下值的下表:
id name farmid amount cycleid totalprice quantity month year
1 DAP 1 2400 1 24000 10 DECEMBER 2012
2 UREA 1 2500 1 5000 2 DECEMBER 2012
3 SENCOR 1 1200 1 2400 2 DECEMBER 2012
4 DAP 2 2400 1 2400 1 DECEMBER 2012
5 UREA 2 2500 1 2400 1 DECEMBER 2012
6 SENCOR 3 2000 2 4000 2 DECEMBER 2012
7 DAP 1 3000 2 3000 1 JANUARY 2013
8 UREA 2 3000 2 6000 2 JANUARY 2013
9 SENCOR 3 3000 2 6000 2 JANUARY 2013
I have a problem on formulating the right SQL statement to do a batch update for example Update rows with month of DECEMBER
year of 2012
and only with farmid 1
NOTE: A farmid row cannot have more than one cycleid in the same month eg in the month of december. 我在制定正确的SQL语句以进行批处理更新时遇到问题,例如使用
2012
DECEMBER
年的月份更新行,并且仅使用Farmid 1
注意:Farmid行在同一月份(例如,当月)不能具有多个cycleid 12月。 Please help to comeup with the right sql statement for the above problem.This is what I tried in code-igniter: 请帮助解决以上问题的正确的sql语句。这是我在代码点火器中尝试过的方法:
$chemicalarray = array(
1=>array('name'=>$B41,'farmid'=>$farmname_id,'amount'=>$D41,'cycleid'=>$cycleid,'totalprice'=>$E41,'quantity'=>$C41,'year'=>$E4,'month'=>$E5),
2=>array('name'=>$B42,'farmid'=>$farmname_id,'amount'=>$D42,'cycleid'=>$cycleid,'totalprice'=>$E42,'quantity'=>$C42,'year'=>$E4,'month'=>$E5),
3=>array('name'=>$B43,'farmid'=>$farmname_id,'amount'=>$D43,'cycleid'=>$cycleid,'totalprice'=>$E43,'quantity'=>$C43,'year'=>$E4,'month'=>$E5),
4=>array('name'=>$B44,'farmid'=>$farmname_id,'amount'=>$D44,'cycleid'=>$cycleid,'totalprice'=>$E44,'quantity'=>$C44,'year'=>$E4,'month'=>$E5),
5=>array('name'=>$B45,'farmid'=>$farmname_id,'amount'=>$D45,'cycleid'=>$cycleid,'totalprice'=>$E45,'quantity'=>$C45,'year'=>$E4,'month'=>$E5),
);
foreach ($chemicalarray as $key) {
$unitsql = "UPDATE chemical SET name = '{$key['name']}', farmid = '{$key['farmid']}' ,
amount = '{$key['amount']}' , cycleid = '{$key['cycleid']}' ,
totalprice = '{$key['totalprice']}', quantity = '{$key['quantity']}'
WHERE year = '{$key['year']}'
AND month = '{$key['month']}'";
$q = mysql_query($unitsql);
echo $key['name'];
echo $key['amount'];
if($q==true){ }else{ echo mysql_error(); }
}
Output of var_dump is :
array(8) { ["name"]=> string(8) "Round Up" ["farmid"]=> string(1) "1" ["amount"]=> float(1300) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(15600) ["quantity"]=> float(12) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(7) "Krismat" ["farmid"]=> string(1) "1" ["amount"]=> float(1500) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(15000) ["quantity"]=> float(10) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(3) "DAP" ["farmid"]=> string(1) "1" ["amount"]=> float(2500) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(75000) ["quantity"]=> float(30) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(4) "Urea" ["farmid"]=> string(1) "1" ["amount"]=> float(2000) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(50000) ["quantity"]=> float(25) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" } Done11array(8) { ["name"]=> string(9) "Dual Gold" ["farmid"]=> string(1) "1" ["amount"]=> float(2500) ["cycleid"]=> string(1) "1" ["totalprice"]=> float(75000) ["quantity"]=> float(30) ["year"]=> float(2012) ["month"]=> string(8) "DECEMBER" }
You are just updating the same exact rows over an over again for each month-year combination. 您只需要为每个月-年组合重新更新相同的确切行。 If you need to limit the updates to only certain
year
- month
- farmid
combinations, then you need to add farmid
in your WHERE clause. 如果您只需要将更新限制为仅特定的
year
- month
- farmid
组合,则需要在WHERE子句中添加farmid
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.