MySQL UPDATE value based on SELECT value of +1 Increase column value

Inquiry:

UPDATE nominees SET votes = ( SELECT votes
FROM nominees
WHERE ID =1 ) +1

Error:

You cannot specify destination nominee tables for updates in FROM

Not sure if there is an error based error there, this is the first time I try to include an inline column, I think you can name it. So I'm obvioulsy doing something wrong, but donโ€™t know how to fix it.

+5
source share
1 answer

Your query is UPDATEmissing a sentence WHERE, so even if MySQL allowed it, the effect would be to find the value votesfor the row, ID =1add 1 to it, and then update everything in the table with the result.

, . ,

UPDATE nominees 
SET votes = votes +1
WHERE ID =1 

, ,

UPDATE nominees
SET    votes = (select votes + 1
                FROM   (SELECT votes
                        FROM   nominees
                        WHERE  ID = 1) T)  

You can't specify target table 'nominees' for update in FROM, .

+12

All Articles