
- 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
Update only a single column in a MySQL table and increment on the basis of a condition
Let us first create a table −
mysql> create table DemoTable ( Name varchar(50), Score int ); Query OK, 0 rows affected (1.02 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Sam',45); Query OK, 1 row affected (0.22 sec) mysql> insert into DemoTable values('Mike',28); Query OK, 1 row affected (0.36 sec) mysql> insert into DemoTable values('Carol',27); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable values('David',67); Query OK, 1 row affected (0.19 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | Sam | 45 | | Mike | 28 | | Carol | 27 | | David | 67 | +-------+-------+ 4 rows in set (0.00 sec)
Following is the query to update only a single column and increment on the basis of a condition −
mysql> update DemoTable set Score=Score+10 where Score < 30; Query OK, 2 rows affected (0.22 sec) Rows matched: 2 Changed: 2 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------+-------+ | Name | Score | +-------+-------+ | Sam | 45 | | Mike | 38 | | Carol | 37 | | David | 67 | +-------+-------+ 4 rows in set (0.00 sec)
- Related Articles
- Update only a single column value in MySQL
- Perform SUM and SUBTRACTION on the basis of a condition in a single MySQL query?
- Best way to update a single column in a MySQL table?
- Update a table in MySQL and display only the initials name in a new column
- Display the student marks in a single column on the basis of subject in MySQL?
- Insertion in a MySQL table with only a single column set as auto_increment?
- Delete only some rows from a table based on a condition in MySQL
- A single MySQL query to update only specific records in a range without updating the entire column
- Update only a single value from a MySQL table where select from same table ordered in descending order?
- How to insert only a single column into a MySQL table with Java?
- Update multiple rows in a single column in MySQL?
- Perform MySQL UPDATE on the basis of DATE value in another column
- How can I update MySQL table after quoting the values of a column with a single quote?
- Updating only a single column value in MySQL?
- MySQL query to fetch only a single field on the basis of boolean value in another field

Advertisements