- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to update a MySQL column by subtracting a value with some conditions?
Let us first create a table −
mysql> create table DemoTable ( Score int ); Query OK, 0 rows affected (0.65 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(45); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(29); Query OK, 1 row affected (0.11 sec) mysql> insert into DemoTable values(56); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(24); Query OK, 1 row affected (0.09 sec) mysql> insert into DemoTable values(32); Query OK, 1 row affected (0.09 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Score | +-------+ | 45 | | 29 | | 56 | | 24 | | 32 | +-------+ 5 rows in set (0.00 sec)
Following is the query to update MySQL column by subtracting a value with some conditions −
mysql> update DemoTable set Score=Score-2 where Score > 30; Query OK, 3 rows affected (0.14 sec) Rows matched: 3 Changed: 3 Warnings: 0
Let us check the table records once again −
mysql> select *from DemoTable;
This will produce the following output −
+-------+ | Score | +-------+ | 43 | | 29 | | 54 | | 24 | | 30 | +-------+ 5 rows in set (0.00 sec)
- Related Articles
- Subtracting a number from a single MySQL column value?
- How to update MySQL column with random value?
- How to change a column in an R data frame with some conditions?
- How to update a MySQL table by swapping two column values?
- Update only a single column value in MySQL
- How to update a specific column value fetched with CASE statement?
- Update a MySQL column with JSON format?
- How to update a MySQL date type column?
- How to update records in a column with random numbers in MySQL?
- Update a column of text with MySQL REPLACE()
- Update a column value, replacing part of a string in MySQL?
- How to update all the entries except a single value in a particular column using MySQL?
- How to update a value with substring of current value by removing the separator and numbers after a separator in MySQL?
- MySQL SELECT to sum a column value with previous value
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?

Advertisements