Jump to content

Moving rows from table1 to table2


DarkReaper

Recommended Posts

[quote author=DarkReaper link=topic=105055.msg419408#msg419408 date=1156167151]
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 :)
[/quote]
[code]
INSERT INTO table 2 SELECT * FROM table1 WHERE id=$idnum
[/code]

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

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.
[code]
REPLACE INTO table2 SELECT * FROM table1 WHERE id = $idnum
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.