• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 989
  • Last Modified:

Update row with another rown from the same table

Using MySQL, I would like to update a row from a table using another row from that same table, is this possible? Ideally something like this:

UPDATE table SET row 1 = row 2
DELETE row 2

I need to do this for many tables and I don't want to have to write a script in PHP for each table. It would be easy enough to read the data into PHP and then run another query to update the first row, but I will then have to do this for each table which is a pain.
0
DarkHill
Asked:
DarkHill
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi DarkHill,

You'll need to join the table (to itself) based on some key.

UPDATE table1,table2 SET table1.column = table2.column
WHERE table1.rowID = table2.rowID;

Of course, the filter can be as complicated as necessary to map the correct rows.  :)


Good Luck,
Kent
0
 
DarkHillAuthor Commented:
Hey Kent,

Thanks, that put me on the right path, I did a test now and this seems to work pretty well:

UPDATE tester t1, tester t2 SET t1.title = t2.title, t1.more = t2.more
WHERE t1.id=1 and t2.id = 2
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now