Skip to content
Advertisement

How to make a SELECT MAX(id) inside UPDATE?

I’m starting in Mysql and I’m trying to find a way to select the maximum id inside update. This is what I tried but it doesn’t work.

UPDATE `chatMessages` 
SET send = 'true' 
WHERE id = (SELECT MAX(id) FROM `chatMessages`)

Advertisement

Answer

Use ORDER BY and LIMIT.

UPDATE `chatMessages` 
SET send = 'true' 
ORDER BY id DESC
LIMIT 1
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement