简而言之,我需要做的是:
- 在表中重复 X 行
- 更改几个字段
- 保持自动递增 ID 的顺序。
这就是我到目前为止所做的。
$this->db->query("CREATE TEMPORARY TABLE tmpTbl AS SELECT * FROM orders_products WHERE op_order_id = $id");
$temp = $this->getAll('tmpTbl');
## grab the highest id in the orders_products table so we can begin auto_inc after that
$this->db->select_max("op_id");
$max = $this->getAll('orders_products');
## set the id to a counter for the loop
$counter = $max[0]->op_id;
## loop through the results editing fields as needed
foreach ( $temp as $t ) :
$counter++;
$this->db->query("UPDATE tmpTbl SET op_id = $counter, op_order_id = $orderId WHERE op_order_id = $id");
endforeach;
$temp = $this->getAll('tmpTbl');
## insert the new duplications into the orders_products table
$this->db->query("INSERT INTO orders_products SELECT * FROM tmpTbl");
## drop the temp table so it is fresh and clean for the next duplication
$this->db->query("DROP TEMPORARY TABLE tmpTbl");
当我运行它时,我会到达这条线
$this->db->query("INSERT INTO orders_products SELECT * FROM tmpTbl");
它向我抛出了这个错误
Duplicate entry '54' for key 'PRIMARY
INSERT INTO orders_products SELECT * FROM tmpTbl
我首先创建计数器的原因是为了解决这个问题。然而,我很遗憾地错了。