简而言之,我需要做的是:
这是我到目前为止所做的事情。
$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
我首先创建计数器的原因是为了解决这个问题。但是,我很遗憾地错了。
答案 0 :(得分:0)
我会做什么在你的情况下是删除ID列。从SELECT中,INSERT将自动生成ID。
INSERT INTO orders_products (column2,column3,etc)
SELECT column2, column3, etc FROM tmpTbl