Solved

add +1 to column value in mySQL

Posted on 2011-09-05
5
308 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 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 63 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 62 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now