Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a number to unknown number in SQL

Tags:

c#

sql

Is it possible to add a number to a unknown number in sql column? For example:

In my sql database is a column named "overallRating". I want to add +1 to whatever number is stored in that column. So if number 14 is stored in overallRating column, +1 would make it 15.

Is this possible to do this without retreving this number from sql first? Something like:

UPDATE MyTable SET overallRating+=1 WHERE url='myurl..'

EDIT When solution was already given:

Actually I haven't tried my code above, but I did now and it also works. I made a syntax error on my first try, that's why I thought it's not working.

like image 779
rootpanthera Avatar asked Jan 11 '23 10:01

rootpanthera


1 Answers

The += syntax is very new to T-SQL and generally not supported by most PROD systems today and so you just need to spell it out:

UPDATE MyTable SET overallRating=overallRating+1 WHERE ...
like image 63
Mike Perrenoud Avatar answered Jan 17 '23 15:01

Mike Perrenoud