Jump to content


Photo

Moving rows from table1 to table2


  • Please log in to reply
2 replies to this topic

#1 DarkReaper

DarkReaper
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 21 August 2006 - 01:32 PM

Lets suppose we have 2 tables with same structure, i want to move row with id=N from table1 to table2. How do i do that? I hope there is a simple way, because selecting from 1 -> inserting in 2 does not appear to be a good idea :)

#2 shoz

shoz
  • Staff Alumni
  • Advanced Member
  • 600 posts

Posted 21 August 2006 - 02:25 PM

Lets suppose we have 2 tables with same structure, i want to move row with id=N from table1 to table2. How do i do that? I hope there is a simple way, because selecting from 1 -> inserting in 2 does not appear to be a good idea :)

INSERT INTO table 2 SELECT * FROM table1 WHERE id=$idnum

If by "move" you mean to remove the entry from table1 as well, then do a DELETE afterwards
DELETE FROM table1 WHERE id = $idnum

If the tables have a PRIMARY KEY and you're fine with overwriting a previous entry with the same id then you should change the INSERT to a REPLACE.
REPLACE INTO table2 SELECT * FROM table1 WHERE id = $idnum


#3 DarkReaper

DarkReaper
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 21 August 2006 - 02:57 PM

Thanks! These examples will do the job :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users