Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a value to existing value using SQL query

I have a table named products.

Products
    Quantity
    5

I need to update Quantity by any value; for example, adding 3 to the current value, giving output like below:

Quantity
8

How can I write an SQL query to accomplish this?

like image 781
user475464 Avatar asked Nov 15 '12 10:11

user475464


People also ask

How do I add data to an existing SQL data?

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

How do you add values to an existing table?

To insert a row into a table, you need to specify three things: First, the table, which you want to insert a new row, in the INSERT INTO clause. Second, a comma-separated list of columns in the table surrounded by parentheses. Third, a comma-separated list of values surrounded by parentheses in the VALUES clause.

How do you edit existing data in SQL?

1. ALTER Command : ALTER is an SQL command used in Relational DBMS and is a Data Definition Language (DDL) statement. ALTER can be used to update the table's structure in the database (like add, delete, drop indexes, columns, and constraints, modify the attributes of the tables in the database).


2 Answers

update products
set quantity = quantity + 3
like image 153
juergen d Avatar answered Oct 25 '22 17:10

juergen d


declare @table table(id int, quantity int)
insert into @table values(1, 5)

update @table
set quantity = quantity + 3
output inserted.quantity

Assuming that you actually wanted the value outputted. Don't forget any where clauses that may be needed

like image 4
Manatherin Avatar answered Oct 25 '22 17:10

Manatherin