简体   繁体   中英

Trying to update a table with a value from the same table

UPDATE seasons SET good=good+1 WHERE id = (SELECT id FROM seasons ORDER BY id DESC LIMIT 1)

When I run this command I get the error:

#1093 - Table 'seasons' is specified twice, both as a target for 'UPDATE' and as a separate source for data

This is a MySQL limitation.

One method is to use JOIN . However, for this it is probably better to use LIMIT and ORDER BY :

UPDATE seasons s 
    SET s.good = s.good + 1 
    ORDER BY id DESC
    LIMIT 1;

This does assume that id is not duplicated in the table, but that seems like a reasonable assumption for a column called id .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM