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
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)
Advertisements
