我有一个表,我想复制表中的特定行。我知道这不是做事的最好方法,但我们正在寻找快速的解决方案。1 W, ^& \" } A7 T# i4 ~
这比我最初想的要难。我要做的就是把整个记录复制到MySql中自动增量表中的新记录不需要指定每个字段。这是因为表将来可能会改变,重复可能会中断。PHP复制MySQL记录。' C, Z- S6 j5 G" Z# ]2 e
因为这是一个问题’SELECT *’查询中,MySql试图复制要复制的记录ID,这将产生重复ID错误。* x9 P# F% W, v' q8 V3 U
阻止: INSERT INTO customer SELECT * FROM customer WHEREcustomerid=9181.它也被封锁了INSERT INTO customer (Field1,Field2,...) SELECTField1,Field2,..... FROM customer WHERE customerid=9181.8 U$ I4 h3 I/ Y, K/ o$ r8 U
没有办法去做PHP或MySQL执行此操作?* U8 G! i+ h# r& G. Q( y/ n3 q& Y$ x
. M5 q8 ^6 a, @, o! \ 解决方案: # E. d" J9 \) v1 R _/ K% E
我终于找到了这个代码。我相信它会帮助未来的人。就是这样。- J; t& B) T3 J$ @- r9 l
function DuplicateMySQLRecord ($table,$id_field,$id) { // load the original record into an array $result = mysql_query("SELECT * FROM {$table} WHERE {$id_field}={$id}"); $original_record = mysql_fetch_assoc($result); // insert the new record and get the new auto_increment id mysql_query("INSERT INTO {$table} (`{$id_field}`) VALUES (NULL)"); $newid = mysql_insert_id(); / generate the query to update the new record with the previous values $query = "UPDATE {$table} SET "; foreach ($original_record as $key => $value) if ($key != $id_field) $query .= '`'.$key.'` = "'.str_replace('"','\"',$value).'", $query = substr($query,0,strlen($query)-2); # lop off the extra trailing comma $query .= " WHERE {$id_field}={$newid}"; mysql_query($query); // return the new id return $newid;}这是文章http://www.epigroove.com/posts/79/how_to_duplicate_a_record_in_mysql_using_php的链接