Jump to content

Archived

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

DarkReaper

Moving rows from table1 to table2

Recommended Posts

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 :)

Share this post


Link to post
Share on other sites
[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]

Share this post


Link to post
Share on other sites

×

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.