Duplicating MySQL rows.

Discussion in 'MySQL' started by Kyrus, Nov 18, 2008.

  1. #1
    Hello there.

    This might be an obvious question, but still I don't know the answer.

    How do I duplicate a MySQL row. Any Query i could write? Something like: DUPLICATE WHERE ID = 'X'

    Would be nice if I could UPDATE too. Changing some values between them.

    Thanks in advance.
     
    Kyrus, Nov 18, 2008 IP
  2. firemarsh

    firemarsh Peon

    Messages:
    153
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Let's say you had the following table[sql]
    colA|colB|colC
    2 | 7 | 9 |
    3 | 8 | 6 |
    1 | 8 | 7 |
    [/sql]

    and you needed insert a duplicate of the row starting which has a 1 in colA, you could do it like this:

    INSERT INTO tbl(colA,colB,colC) select * from tbl where colA='1';

    BUT, you would have to make sure your database does allow duplicates, and think about turning autocommit off and back on after.
     
    firemarsh, Nov 19, 2008 IP
  3. RichardG87

    RichardG87 Peon

    Messages:
    134
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    INSERT [LOW_PRIORITY | DELAYED | HIGH_PRIORITY] [IGNORE]
    [INTO] tbl_name [(col_name,...)]
    {VALUES | VALUE} ({expr | DEFAULT},...),(...),...
    [ ON DUPLICATE KEY UPDATE
    col_name=expr
    [, col_name=expr] ... ]

    pls try this...
     
    RichardG87, Nov 19, 2008 IP