Development PHP/MySQL: No easy way to move row between tables?

Discussion in 'Software' started by jezmck, 6 Nov 2006.

  1. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    I don't think there is, but don't want to waste my time coding it.
    (I can't use anything third party atm)
     
  2. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    How do you mean? Just moving a row across should take a few queries (or one if you exceptionally lazy)

    Code:
    LOCK TABLE src_table; INSERT INTO `dest_table` (c1, c2, c3) SELECT f1, f2, f3 FROM `src_table` WHERE condition; DELETE FROM `src_table` WHERE condition; UNLOCK TABLE src_table;
     
  3. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    the problem is that these records have about 30 fields, so writing it all out would be a real pain.

    the tables are identical, so yes this is ought to be solved by having a status field, but it's out of my hands.
     
  4. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    try

    Code:
    INSERT INTO `dest` SELECT * FROM `source`
     
  5. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    blimey, never expected that to work!
    unfortunately, I don't think I can use this as there may be clashes with unique IDs. :(
     
    Last edited: 6 Nov 2006
  6. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    I was going to use something along the lines of
    PHP:
    $get mysql_query("SELECT * FROM `source` WHERE ...");
    $got mysql_fetch_assoc($get);
    $qry "INSERT INTO `dest` ('".join("', '"array_keys($got))
     .
    "') VALUES (".join(", "$got).")";
    but this doesn't allow me to alter/omit any values, and is therefore no better than your idea!

    Perhaps if I make my own join() style function with a foreach($a as $k => $v) {}

    Any other ideas?
     
    Last edited: 6 Nov 2006
  7. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    surely implode() would be better?
     
  8. jezmck

    jezmck Minimodder

    Joined:
    25 Sep 2003
    Posts:
    4,456
    Likes Received:
    36
    join is just an alias of implode, I just prefer the word.
     
  9. DougEdey

    DougEdey I pwn all your storage

    Joined:
    5 Jul 2005
    Posts:
    13,933
    Likes Received:
    33
    foreach should do you, or even a for loop, will make it easier to jump the requested values
     

Share This Page