Solved

add +1 to column value in mySQL

Posted on 2011-09-05
5
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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