The Query:
UPDATE nominees SET votes = ( SELECT votes
FROM nominees
WHERE ID =1 ) +1
The Error:
You can't specify target table 'nominees' for update in FROM
Not sure whats wrong there based on the error, this is the first time im tryin to incriment a column inline i guess you can call it. So I am obvioulsy doing something wrong but dont know how to fix it.
Just add them together: UPDATE LOG SET TIME_EXIT = '2013/02/22' WHERE ID = ( SELECT ID FROM employee ORDER BY ID DESC LIMIT );
MySQL UPDATE command can be used to update multiple columns by specifying a comma separated list of column_name = new_value. Where column_name is the name of the column to be updated and new_value is the new value with which the column will be updated.
The SET clause indicates which columns to modify and the values they should be given. Each value can be given as an expression, or the keyword DEFAULT to set a column explicitly to its default value.
Your UPDATE
query is missing any WHERE
clause so even if MySQL had allowed it then the effect would be to find the votes
value for the ID =1
row add 1 to it then update all rows in the table with the result.
I suspect that was not the desired behaviour. To increment the column value you just need to do
UPDATE nominees
SET votes = votes +1
WHERE ID =1
Just in case you do want the other behaviour you would need to do
UPDATE nominees
SET votes = (select votes + 1
FROM (SELECT votes
FROM nominees
WHERE ID = 1) T)
This wrapping into a derived table avoids the You can't specify target table 'nominees' for update in FROM
error you were getting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With