Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update field to add value to existing value?

Tags:

sql

php

mysql

How to update field to add value to existing value?
For example I have

Table name: table

id   credit 1      4 2      5 3      3 

Is there a way to simply add value to the credit?
like

UPDATE table SET credit = '+7' WHERE id='1'  

I want to add 7 to 4 so that the credit=11 where id='1'
How to do this?

like image 303
Samsan Phone Number Lookup Avatar asked Oct 05 '12 08:10

Samsan Phone Number Lookup


People also ask

How do I add a value to an existing column?

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

How do you update a value in an existing column in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.


2 Answers

UPDATE table SET credit = credit + 7 WHERE id = 1 
like image 96
MatthewMcGovern Avatar answered Sep 30 '22 01:09

MatthewMcGovern


This is just a simple UPDATE. Try the following.

UPDATE tableName SET Credit = Credit + 7 WHERE ID = 1 

note that ID = 1 and ID = '1' is the same as the server automatically parses it.

like image 20
John Woo Avatar answered Sep 30 '22 03:09

John Woo