
- 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 perform update in MySQL to disallow incrementing all the values above a specific value?
Let us first create a table −
mysql> create table DemoTable -> ( -> Value int -> ); Query OK, 0 rows affected (0.54 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values(100); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable values(150); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(180); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable values(200); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
Output
+-------+ | Value | +-------+ | 100 | | 150 | | 180 | | 200 | +-------+ 4 rows in set (0.00 sec)
Following is the query to UPDATE MySQL max value −
mysql> set @value:=45; Query OK, 0 rows affected (0.00 sec) mysql> update DemoTable -> set Value = LEAST(value + @value, 200) ; Query OK, 3 rows affected (0.21 sec) Rows matched: 4 Changed: 3 Warnings: 0
Let us check table records once again −
mysql> select *from DemoTable;
Output
This will produce the following output. Here, we are disallowing incrementing a value to be above 200 −
+-------+ | Value | +-------+ | 145 | | 195 | | 200 | | 200 | +-------+ 4 rows in set (0.00 sec)
- Related Articles
- How to perform Increment in MySQL Update?
- Update all the zero values with a custom value in MySQL with a function similar to ISNULL()
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?
- Incrementing a date in JavaScript, in order to update MongoDB?
- Update all the values of a field with a specific string in MongoDB?
- In MySQL, how to remove the specific prefix from entire column’s value and update them?
- How to update all the entries except a single value in a particular column using MySQL?
- How to reset auto-incrementing column in MySQL?
- How can we update any value in MySQL view as we can update the values in MySQL table?
- How to run MongoDB query to update only a specific field value?
- Perform MySQL UPDATE on the basis of DATE value in another column
- Update all varchar column rows to display values before slash in MySQL?
- How to update a specific column value fetched with CASE statement?
- How to work with auto incrementing column in MySQL?
- How to update field to add value to existing value in MySQL?

Advertisements