Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

add +1 to column value in mySQL

Posted on 2011-09-05
5
Medium Priority
?
319 Views
Last Modified: 2012-05-12
How would I add +1 value to an existing column value in my table? Feel like this is a easy question but I haven't done it for a while and forgot the syntax to do so.

I tried...

UPDATE user_projects
SET views = views + 1    

Didn't work.

Thx for the help!
0
Comment
Question by:brihol44
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:sajayc
ID: 36485880
You query looks fine, the only thing I can think of if its not working is that views is a reserved word.
Therefore you need to put [ ] brackets around the field name.

E.g
UPDATE user_projects
SET [views] = [views] + 1    

Can you try this and see if it works?
0
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 252 total points
ID: 36485887
I believe that without a WHERE clause to limit it, you are going to add 1 to 'views' in every row in that table.
0
 

Author Comment

by:brihol44
ID: 36485908
Good catch... I didn't copy the complete query statement on accident...

I tried the above solution and that didn't work. Any other thoughts?

UPDATE user_projects
SET views = views + 1  
WHERE project_ID = '#project_ID#'

Thx,

Brian
0
 
LVL 3

Accepted Solution

by:
sajayc earned 248 total points
ID: 36485950
Hi,
You only need the quotes around the variable if it is a string.  ID values are usually integers so you can insert without the quotes.  Can you try the query below?

UPDATE user_projects
SET views = views + 1  
WHERE project_ID = #project_ID#

0
 

Author Comment

by:brihol44
ID: 36486020
My IDs are like so... B690F14E-08-08-11... So I do need the quotes. Looks like everything is working though now. I'll give everybody equal points.

Thank you!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

927 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question