Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
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)
Advertisements
