0

我有下表,其中包含以下值:

                        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

我在制定正确的 SQL 语句以进行批量更新时遇到问题,例如使用年的月份更新行DECEMBER并且2012仅使用农场1注意:农场行在同一个月内不能有多个 cycleid,例如在 12 月。请帮助为上述问题提出正确的 sql 语句。这是我在 code-igniter 中尝试的:

     $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" }
4

1 回答 1

0

您只是为每个月-年组合一遍又一遍地更新相同的确切行。如果您需要将更新限制为仅某些year- month-farmid组合,那么您需要添加farmidWHERE 子句。

于 2013-01-29T23:06:37.853 回答