SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot xPox's Avatar
    Join Date
    Sep 2005
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Update only 1 row

    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?

  2. #2
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this?

    UPDATE tablename SET column='newvalue' WHERE id=(SELECT id FROM tablename WHERE column='oldvalue' ORDER BY id ASC LIMIT 1)

  3. #3
    SitePoint Zealot xPox's Avatar
    Join Date
    Sep 2005
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So LIMIT works on UPDATE queries as well as SELECT queries? I wasn't sure about this.

  4. #4
    SitePoint Member
    Join Date
    Jan 2006
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  5. #5
    reads the ********* Crier silver trophybronze trophy longneck's Avatar
    Join Date
    Feb 2004
    Location
    Tampa, FL (US)
    Posts
    9,854
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by xPox
    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?
    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.html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •