MySQL Update Query using SELECT Statement

Posted on 2010-01-07
Last Modified: 2012-05-08

I am attempting to update a number of rows in a MySQL table with values from other rows within the same table and my attempts have failed. My attempt is below however it delivers the error message listed below the query.

UPDATE `Table` set `field_value` =
(SELECT `field_value` FROM `Table` WHERE `field_name` = 'xyz')
WHERE `field_name` = 'abc'

#1093 - You can't specify target table 'Table' for update in FROM clause

Basically I am attempting to use the Select Query to create the variable to use in the update Query.

Is this at all possible?

Question by:FLOG51
    LVL 11

    Accepted Solution


    update src set fldname=tab.fldname from mytable src
    inner join mytable tab on src.idfield=tab.idfield
    and tab.fldname='xyz'
    where src.fldname='abc'

    Hope this helps
    LVL 20

    Assisted Solution

    LVL 26

    Assisted Solution

    try this

    UPDATE `Table` t1, `Table` t2  
    set t1.`field_value` = t2.`field_value`
    WHERE t1.`field_name` = 'abc'
      AND t1.`field_name` = 'xyz'
    LVL 20

    Assisted Solution

    Also -

    "In MySQL, you can't modify the same table which you use in the SELECT part.
    This behaviour is documented at:"

    Author Comment

    Hi rajvja

    Am I correct in assuming that you have used two tables in this query i.e. "src" and "tab"

    I am only using one table which has the data I wish to use to update values in the same Table.


    Author Closing Comment

    Thanks for the attempted help guys, as 'a_b' has stated this is possible from multiple tables but not from one.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
    This article describes some very basic things about SQL Server filegroups.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    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

    11 Experts available now in Live!

    Get 1:1 Help Now