【问题标题】:How to store insert array values as separate record如何将插入数组值存储为单独的记录
【发布时间】:2015-09-25 23:53:11
【问题描述】:

我在 mySql 表中有一个字段,即 Expanse 和另一个名为 Amount 的字段。

现在我想在';'的基础上打破expanse和amount字段中的字符串并将每个记录存储为单独的记录,以便其他字段(即 id 和 name)中的数据保持不变。这样

我正在从原始表中获取记录,并希望将这个所需的结果存储在临时表中。在从原始表中获取记录时,我在';'的基础上爆炸了扩展和数量字符串。

 $query="SELECT * FROM table";
 $result=mysql_query($query);
 while ($row=mysql_fetch_array($result)) 
{
$newid=$row["id"];
$temp=explode(';',$row["expanse"]); 
$new=array_unique($temp);
//what to do after this ?


$temp2=explode(';',$row["amount"]); 
$new2=array_unique($temp2);
//what to do after this ? 
$query2="INSERT INTO table2 (id, $expanse,$amount)  VALUES   ('$newid','$new',$new2);
$result2=mysql_query($query2);
}

//after this i'll be inserting values in these variables into new temporary table that is having same structure so that my orignal data remain unchanged and new data as my need inserted into temporary table.

希望你能得到我的问题。

【问题讨论】:

  • 在 $result2 写入第二个查询以插入临时数据之后。
  • 您可以根据需要保存它,但如果 idprimary key 那么它将始终是唯一的

标签: php mysql arrays optimization


【解决方案1】:

您需要迭代抛出每个记录并再次迭代抛出每个; 分隔值。 idname 对于每条记录都是相同的,但 expanseamount 会有所不同。插入查询的数量取决于; 分隔值的数量

 $query="SELECT * FROM table";
 $result=mysql_query($query);

 while ($row=mysql_fetch_array($result)) 
{

    $id = $row["id"]; // duplicating for each 
    $name = $row['name']; // duplicating for each
    $expance_array = explode(';',$row["expanse"]); 
    $amount_array = explode(';',$row["amount"]); 

    /* count of expance_array and amount_array will be same always as per your idea */

    for($i=0;$i<count($expance_array);++$i) {

        $expanse = $expance_array[$i]; 
        $amount  =  $amount_array[$i];

        /* now insert $id   $name  $expanse  $amount  into the table*/
        $query2="INSERT INTO table2 (id, name , expanse, amount)  VALUES ($id,'$name','$expanse' , $amount");
        $result2=mysql_query($query2);

    }

}   

【讨论】:

  • 我明白了你的逻辑。但是如果 db 中存在多个记录,你能告诉它会起作用吗?在我的情况下,我的原始表 1 中可能有数千条记录。谢谢
  • 是的,它会起作用。因为 while 循环的每次迭代都代表不同的记录。如果您有 1000 条记录并且您的费用为 lunch;dinner;tea,则需要 3 * 1000 = 3000 次插入查询。
【解决方案2】:

您将需要为数组中的每个项目单独插入。

function getInsertQueries($id, $expanse, $amount){
    $insertQueries = array();
    $expanses = array_unique(explode(";", $expanse));
    $amounts = array_unique(explode(";", $amount));
    if(count($expanses) == count($amounts)){
        for($i=0;$i<count($expanses);$i++){
            $insertQueries[] = "INSERT INTO table2 (id, expanse, amount) VALUES ('$id', '$expanses[$i]', '$amounts[$i]')";
        }
    }
    return $insertQueries;
}


$query="SELECT * FROM table";
$result=mysql_query($query);
while ($row=mysql_fetch_array($result)){
    $insertQueries = getInsertQueries($row["id"], $row["expanse"], $row["amount"]);
    foreach($insertQueries as $query){
        mysqli_query($query);
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    • 2016-05-01
    • 2011-05-16
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多