
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How to update field to add value to existing value in MySQL?
You can update field to add value to an existing value with the help of UPDATE and SET command. The syntax is as follows −
UPDATE yourTableName SET yourColumnName = yourColumnName+integerValueToAdd WHERE yourCondition;
To understand the above syntax, let us create a table. The query to create a table is as follows −
mysql> create table addingValueToExisting -> ( -> Id int NOT NULL AUTO_INCREMENT, -> Name varchar(30), -> GameScore int, -> PRIMARY KEY(Id) -> ); Query OK, 0 rows affected (0.58 sec)
Insert records in the table using insert command. The query is as follows −
mysql> insert into addingValueToExisting(Name,GameScore) values('John',89); Query OK, 1 row affected (0.11 sec) mysql> insert into addingValueToExisting(Name,GameScore) values('Mike',56); Query OK, 1 row affected (0.28 sec) mysql> insert into addingValueToExisting(Name,GameScore) values('Sam',99); Query OK, 1 row affected (0.18 sec) mysql> insert into addingValueToExisting(Name,GameScore) values('Carol',100); Query OK, 1 row affected (0.17 sec) mysql> insert into addingValueToExisting(Name,GameScore) values('David',67); Query OK, 1 row affected (0.25 sec) mysql> insert into addingValueToExisting(Name,GameScore) values('Bob',78); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement. The query is as follows −
mysql> select *from addingValueToExisting;
The following is the output −
+----+-------+-----------+ | Id | Name | GameScore | +----+-------+-----------+ | 1 | John | 89 | | 2 | Mike | 56 | | 3 | Sam | 99 | | 4 | Carol | 100 | | 5 | David | 67 | | 6 | Bob | 78 | +----+-------+-----------+ 6 rows in set (0.00 sec)
Update field to add a value to existing value. For our example, let us update 100 with the value 110 by adding 10. The query is as follows −
mysql> update addingValueToExisting set GameScore = GameScore+10 where Id = 4; Query OK, 1 row affected (0.23 sec) Rows matched − 1 Changed − 1 Warnings − 0
Check the specific record has been updated or not. The query is as follows −
mysql> select *from addingValueToExisting where Id = 4;
The following is the output −
+----+-------+-----------+ | Id | Name | GameScore | +----+-------+-----------+ | 4 | Carol | 110 | +----+-------+-----------+ 1 row in set (0.00 sec)
Look at the above output, the value 100 is incremented by 10, which is now 110.
- Related Articles
- Add to existing value in MySQL column using CONCAT function?
- How to add new value to an existing array in JavaScript?
- How to update a field with a particular value if it is null in MySQL?
- How to set default Field Value in MySQL?
- How to update MySQL column with random value?
- How to run MongoDB query to update only a specific field value?
- Reset MySQL field to default value?
- How to sum based on field value in MySQL?
- How to swap a specific field value in MySQL?
- How to add a random number between 30 and 300 to an existing field in MySQL?
- How to add +1 to existing MySQL values?
- How to derive value of a field from another field in MySQL?
- How to make an existing field Unique in MySQL?
- How to add a field with static value to MongoDB find query?
- How to perform custom sort by field value in MySQL?

Advertisements