Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-24-2012, 04:07 PM   PM User | #1
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
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.
shaunthomson is offline   Reply With Quote
Old 09-24-2012, 06:20 PM   PM User | #2
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
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.
guelphdad is offline   Reply With Quote
Users who have thanked guelphdad for this post:
shaunthomson (09-27-2012)
Old 09-27-2012, 08:07 AM   PM User | #3
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
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.
shaunthomson is offline   Reply With Quote
Old 09-27-2012, 12:43 PM   PM User | #4
guelphdad
Super Moderator


 
guelphdad's Avatar
 
Join Date: Mar 2006
Location: St. Catharines, Ontario Canada
Posts: 2,629
Thanks: 4
Thanked 147 Times in 138 Posts
guelphdad will become famous soon enoughguelphdad will become famous soon enough
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.
guelphdad is offline   Reply With Quote
Users who have thanked guelphdad for this post:
shaunthomson (10-02-2012)
Old 09-27-2012, 04:45 PM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
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.
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Users who have thanked abduraooft for this post:
shaunthomson (10-02-2012)
Old 10-02-2012, 02:05 AM   PM User | #6
shaunthomson
New Coder

 
Join Date: May 2012
Posts: 89
Thanks: 51
Thanked 0 Times in 0 Posts
shaunthomson is an unknown quantity at this point
Thanks guys

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

Cheers
shaunthomson is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:17 PM.


Advertisement
Log in to turn off these ads.