Is there a way to limit an UPDATE query to only the first row it finds, instead of updating all rows that match the query?
| SitePoint Sponsor |
Is there a way to limit an UPDATE query to only the first row it finds, instead of updating all rows that match the query?
Something like this?
UPDATE tablename SET column='newvalue' WHERE id=(SELECT id FROM tablename WHERE column='oldvalue' ORDER BY id ASC LIMIT 1)
So LIMIT works on UPDATE queries as well as SELECT queries? I wasn't sure about this.
Well it doesn't work on update queries directly.. In that example you're doing a subselect which has a LIMIT, and then passing that one primary key back to the UPDATE statement.

limit does work on an update statement. did you try it before posting your question? or looking at the mysql manual? if you, the very first sytax chart shows limit. http://dev.mysql.com/doc/refman/5.0/en/update.htmlOriginally Posted by xPox
Bookmarks