Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql server query to add value to existing value

Tags:

sql

sql-server

I have a table with few columns, one of these columns called _bandwidth and the value inside it is decimal.

So i wanna type a sql query that adds values to existing value.

Say the value of _bandwidth of user id 1 is 150.000 and i wanna add 200 to this value so sum would be 350.000

This is the query i typed but it didin't work.

update users set _bandwidth = (select _bandiwdth from users where _ID = 1) + 150 where _ID = 1

Also did something like:

update users set _bandwidth += 200 where _ID = 1

Of course they are wrong, but i hope you understand what i wanna achieve.

Thanks a lot in advance.

EDIT Found the solution and the answer would be:

update users set _bandwidth = _bandwidth + 200 where _ID = 1
like image 246
BOSS Avatar asked Nov 12 '13 18:11

BOSS


People also ask

How can add value to another value in SQL?

INSERT INTO Syntax Specify both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3, ...)

How append data with existing data in SQL?

On the Home tab, in the View group, click View, and then click Design View. On the Design tab, in the Query Type group, click Append. The Append dialog box appears. Next, you specify whether to append records to a table in the current database, or to a table in a different database.

How do you add a new value to an existing table?

There are two ways of using INSERT INTO statement for inserting rows: Only values: First method is to specify only the value of data to be inserted without the column names. INSERT INTO table_name VALUES (value1, value2, value3,…); table_name: name of the table.


1 Answers

UPDATE Users
SET _bandwidth = _bandwidth + 200
WHERE _ID =1

would work

like image 60
Jack Jiang Avatar answered Sep 22 '22 17:09

Jack Jiang