Solved

add +1 to column value in mySQL

Posted on 2011-09-05
5
313 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 83

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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

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. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

821 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