Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts

    Multiple row updates

    Gidday

    Just wondering how, using one query, to update a counter column with + 1 in multiple rows when the row ids are supplied.

    So:

    UPDATE table
    SET col=col+1
    WHERE id in (1,2,3)

    This doesn't work. It does work if the value of col is static. Is it possible, or will I have to create a separate query for each row update?

    Thanks for your help.

  2. #2
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,668
    Thanks
    6
    Thanked 150 Times in 141 Posts
    what doesn't work about it?
    It would increment the value of col by 1 for for each of id=1, id=2 and id=3.

  3. Users who have thanked guelphdad for this post:

    shaunthomson (09-27-2012)

  4. #3
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts
    That's what I thought too, but it's not working. I going to go back over my code - I think something I've done is screwy.

  5. #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,668
    Thanks
    6
    Thanked 150 Times in 141 Posts
    So either
    a) you haven't shown your actual query you are having trouble with
    b) you've introduced a front end application code that we don't know about

    Either or it is tougher for us to help you with the larger picture.

  6. Users who have thanked guelphdad for this post:

    shaunthomson (10-02-2012)

  7. #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    15,006
    Thanks
    164
    Thanked 2,244 Times in 2,231 Posts
    UPDATE table
    SET col=col+1
    WHERE id in (1,2,3)
    What's the actual name of that table?
    The name table is a mysql keyword. You shouldn't name your table by that word.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  8. Users who have thanked abduraooft for this post:

    shaunthomson (10-02-2012)

  9. #6
    New Coder
    Join Date
    May 2012
    Posts
    89
    Thanks
    51
    Thanked 0 Times in 0 Posts
    Thanks guys

    I had an error elsewhere in my code. Plus, your comments have be taken in.

    Cheers


 

Posting Permissions

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