简体   繁体   中英

mysql update query with select as criteria

UPDATE reservation SET flag = "1" WHERE ipAddress = (SELECT ipAddress FROM reservation WHERE endDate < CURRENT_TIMESTAMP);

Im trying to use this query for changing flag column of those entries in reservation table whose date has expired. The flag column is by default 0. so im trying to change expired once to 1 for my identification.

Im getting the following error.

ERROR 1093 (HY000): You can't specify target table 'reservation' for updatein FROM clause

Can someone suggest a solution to this problem..

if you're using the same table in the subselect, you can probably omit the subselect altogether. why dont you use the following instead

UPDATE reservation 
SET flag = "1" 
WHERE endDate < CURRENT_TIMESTAMP

You do not need the inner SELECT . You just need to update the flag status based on the endDate

UPDATE reservation 
SET flag = "1" 
WHERE endDate < CURRENT_TIMESTAMP

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