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
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 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.

  • Users who have thanked guelphdad for this post:

    shaunthomson (09-27-2012)

  • #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.

  • #4
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 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.

  • Users who have thanked guelphdad for this post:

    shaunthomson (10-02-2012)

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    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)

  • Users who have thanked abduraooft for this post:

    shaunthomson (10-02-2012)

  • #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
    •