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
A single MySQL query to update only specific records in a range without updating the entire column
Let us first create a table −
mysql> create table DemoTable ( Name varchar(40), Position int ); Query OK, 0 rows affected (1.17 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable values('Chris',90);
Query OK, 1 row affected (0.20 sec)
mysql> insert into DemoTable values('David',67);
Query OK, 1 row affected (0.18 sec)
mysql> insert into DemoTable values('Bob',55);
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values('Sam',40);
Query OK, 1 row affected (0.15 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable;
This will produce the following output −
+-------+----------+ | Name | Position | +-------+----------+ | Chris | 90 | | David | 67 | | Bob | 55 | | Sam | 40 | +-------+----------+ 4 rows in set (0.00 sec)
Following is the query to update only specific records in a range without updating the entire column −
mysql> update DemoTable set Position=Position+10 where Position > 50 and Position < 90; Query OK, 2 rows affected (0.11 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 | Position | +-------+----------+ | Chris | 90 | | David | 77 | | Bob | 65 | | Sam | 40 | +-------+----------+ 4 rows in set (0.00 sec)
Advertisements
