dbTalk Databases Forums  

Update from one table to another, then delete matched row from old?

comp.databases.mysql comp.databases.mysql


Discuss Update from one table to another, then delete matched row from old? in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Jason C
 
Posts: n/a

Default Update from one table to another, then delete matched row from old? - 11-03-2011 , 09:55 PM






OK, this is sort of an addendum to the other "Update from one table to another" thread, but it's a little different so I thought that future readers might find it easier in a different thread.

I'm using this to copy matching rows from one table to another:

UPDATE t1, t2
SET t1.comment = t2.comment
WHERE t1.username = t2.username
AND t1.postdate = t2.postdate

Table t2 is an old backup of t1, so they have an identical structure.

Now, I'm wanting to modify the code to update like it is, but then when an update is made, delete the corresponding row from t2.

This way, t1 would be completely updated, and t2 would only have rows in it where no matching row was found in t1.

My thought was to add a dummy column to t2, then change the script to:

SET t1.comment = t2.comment, t2.dummy="1"

Then, after the query runs, a second query of:

DELETE FROM t2 WHERE dummy="1"

For the sake of knowledge and efficiency, though, is there a better / easier way to do this in a single query?

Reply With Quote
  #2  
Old   
Luuk
 
Posts: n/a

Default Re: Update from one table to another, then delete matched row fromold? - 11-04-2011 , 12:46 PM






On 04-11-2011 04:55, Jason C wrote:
Quote:
OK, this is sort of an addendum to the other "Update from one table to another" thread, but it's a little different so I thought that future readers might find it easier in a different thread.

I'm using this to copy matching rows from one table to another:

UPDATE t1, t2
SET t1.comment = t2.comment
WHERE t1.username = t2.username
AND t1.postdate = t2.postdate

Table t2 is an old backup of t1, so they have an identical structure.

Now, I'm wanting to modify the code to update like it is, but then when an update is made, delete the corresponding row from t2.

This way, t1 would be completely updated, and t2 would only have rows in it where no matching row was found in t1.

My thought was to add a dummy column to t2, then change the script to:

SET t1.comment = t2.comment, t2.dummy="1"

Then, after the query runs, a second query of:

DELETE FROM t2 WHERE dummy="1"

For the sake of knowledge and efficiency, though, is there a better / easier way to do this in a single query?

DELETE FROM t2
WHERE (username, postdate)
IN (SELECT username, postdate
FROM t1 WHERE t2.comment=t1.comment)


--
Luuk

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.