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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.