In MySQL, can I copy one row to insert into the same table?

后端 未结 26 2361
-上瘾入骨i
-上瘾入骨i 2020-11-27 09:56
insert into table select * from table where primarykey=1

I just want to copy one row to insert into the same table (i.e., I want to duplicate an ex

26条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 10:17

    Some of the following was gleaned off of this site. This is what I did to duplicate a record in a table with any number of fields:

    This also assumes you have an AI field at the beginning of the table

    function duplicateRow( $id = 1 ){
    dbLink();//my db connection
    $qColumnNames = mysql_query("SHOW COLUMNS FROM table") or die("mysql error");
    $numColumns = mysql_num_rows($qColumnNames);
    
    for ($x = 0;$x < $numColumns;$x++){
    $colname[] = mysql_fetch_row($qColumnNames);
    }
    
    $sql = "SELECT * FROM table WHERE tableId = '$id'";
    $row = mysql_fetch_row(mysql_query($sql));
    $sql = "INSERT INTO table SET ";
    for($i=1;$i

提交回复
热议问题