
- 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
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)
- Related Articles
- Updating only a single column value in MySQL?
- MySQL update multiple records in a single query?
- Update only a single column value in MySQL
- MySQL query to update only a single field in place of NULL
- MySQL query to select all the records only from a specific column of a table with multiple columns
- MongoDB query to update only a single item from voting (up and down) records?
- How to update a range of records in MySQL?
- MySQL query to display only the records that contains single word?
- How to display records having sum between a specific range using GROUP BY, HAVING and ORDER BY in a single MySQL query?
- Get multiple count in a single MySQL query for specific column values
- MongoDB Query to search for records only in a specific hour?
- Update only a single column in a MySQL table and increment on the basis of a condition
- MySQL query to remove Null Records in a column?
- Update only a single MongoDB document without deleting any date
- Update two columns with a single MySQL query

Advertisements