
- 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 update a range of records in MySQL?
To update a range of records in MySQL, you can use BETWEEN. Let us first create a table:
mysql> create table DemoTable ( Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, Name varchar(20), Age int ); Query OK, 0 rows affected (0.53 sec)
Following is the query to insert some records in the table using insert command:
mysql> insert into DemoTable(Name,Age) values('Larry',23); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name,Age) values('Sam',24); Query OK, 1 row affected (0.21 sec) mysql> insert into DemoTable(Name,Age) values('Chris',21); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name,Age) values('Carol',25); Query OK, 1 row affected (0.15 sec) mysql> insert into DemoTable(Name,Age) values('David',22); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name,Age) values('Robert',26); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name,Age) values('John',20); Query OK, 1 row affected (0.10 sec) mysql> insert into DemoTable(Name,Age) values('Mike',27); Query OK, 1 row affected (0.13 sec) mysql> insert into DemoTable(Name,Age) values('Johnny',28); Query OK, 1 row affected (0.14 sec) mysql> insert into DemoTable(Name,Age) values('James',23); Query OK, 1 row affected (0.23 sec)
Following is the query to display records from the table using select command:
mysql> select *from DemoTable;
This will produce the following output:
+----+--------+------+ | Id | Name | Age | +----+--------+------+ | 1 | Larry | 23 | | 2 | Sam | 24 | | 3 | Chris | 21 | | 4 | Carol | 25 | | 5 | David | 22 | | 6 | Robert | 26 | | 7 | John | 20 | | 8 | Mike | 27 | | 9 | Johnny | 28 | | 10 | James | 23 | +----+--------+------+ 10 rows in set (0.00 sec)
Following is the query to update a range of records in MySQL. We are updating the Name to ‘Bob’ for Ids in the range 5 to 10:
mysql> update DemoTable set Name='Bob', Age=23 where Id between 5 AND 10; Query OK, 6 rows affected (0.25 sec) Rows matched: 6 Changed: 6 Warnings: 0
Let us now display all records including updated records:
mysql> select *from DemoTable;
This will produce the following output
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | Larry | 23 | | 2 | Sam | 24 | | 3 | Chris | 21 | | 4 | Carol | 25 | | 5 | Bob | 23 | | 6 | Bob | 23 | | 7 | Bob | 23 | | 8 | Bob | 23 | | 9 | Bob | 23 | | 10 | Bob | 23 | +----+-------+------+ 10 rows in set (0.00 sec)
- Related Articles
- A single MySQL query to update only specific records in a range without updating the entire column
- How to update records in a column with random numbers in MySQL?
- MySQL query to fetch records from a range of months?
- MySQL update multiple records in a single query?
- MySQL queries to update date records with NULL values
- MySQL Stored Procedure to update records with certain condition?
- MySQL edit and update records including employee salary
- MongoDB query to get date records in a range
- MySQL query to get the dates between range of records displaying student’s Date of Birth?
- Program to update elements in a given range in Python
- How to update a timestamp field of a MySQL table?
- Selecting records within a range and with a condition set on two columns in MySQL?
- How does MongoDB Update() method work to set records of entire field?
- How to update two columns in a MySQL database?
- Update tag records in MongoDB quickly

Advertisements