Update row with another rown from the same table

Posted on 2009-02-12
Last Modified: 2012-05-06
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.
Question by:DarkHill
    LVL 45

    Accepted Solution

    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,
    LVL 7

    Author Comment

    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 and = 2

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now