
- 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
Is it possible to use UPDATE query with LIMIT in MySQL?
Yes, it is possible to use UPDATE query with LIMIT in MySQL. Let us see how.
For our example, we will first create a table. The CREATE command is used to create a table.
mysql>CREATE table tblUpdateLimit -> ( -> id int, -> name varchar(100) -> ); Query OK, 0 rows affected (0.53 sec)
Records are inserted with the help of INSERT command.
mysql>INSERT into tblUpdateLimit values(1,'John'); Query OK, 1 row affected (0.54 sec) mysql>INSERT into tblUpdateLimit values(2,'Carol'); Query OK, 1 row affected (0.12 sec) mysql>INSERT into tblUpdateLimit values(3,'Smith'); Query OK, 1 row affected (0.10 sec) mysql>INSERT into tblUpdateLimit values(4,'Kayle'); Query OK, 1 row affected (0.44 sec) mysql>INSERT into tblUpdateLimit values(5,'David'); Query OK, 1 row affected (0.13 sec) mysql>INSERT into tblUpdateLimit values(6,'Jason'); Query OK, 1 row affected (0.18 sec) mysql>INSERT into tblUpdateLimit values(7,'Larry'); Query OK, 1 row affected (0.15 sec) mysql>INSERT into tblUpdateLimit values(8,'Serhat'); Query OK, 1 row affected (0.15 sec) mysql>INSERT into tblUpdateLimit values(9,'Winny'); Query OK, 1 row affected (0.18 sec)
To display the above table, here is the query.
mysql> SELECT *from tblUpdateLimit;
The following is the output.
+------+--------+ | id |name | +------+--------+ | 1 | John | | 2 | Carol | | 3 | Smith | | 4 | Kayle | | 5 | David | | 6 | Jason | | 7 | Larry | | 8 | Serhat | | 9 | Winny | +------+--------+ 9 rows in set (0.00 sec)
Let us now see the syntax to use UPDATE query with limit.
UPDATE yourTableName SET column_name='some value’' WHERE column_name1 IN ( SELECT column_name1 FROM ( select column_name1 from yourTableName order by column_name1 asc limit integerValue,integerValue) anyAliasName );
Implementing the query now to fulfil our purpose and using it to set the name ‘Adam’, with limit 7.
mysql> UPDATE tblUpdateLimit SET name = 'Adam' -> WHERE id IN ( SELECT id FROM ( select id from tblUpdateLimit order by id asc limit 0,7)l); Query OK, 7 rows affected (0.27 sec) Rows matched: 7 Changed: 7 Warnings: 0
Check whether the table is updated or not.
mysql> SELECT *from tblUpdateLimit;
Here is the output.
+------+--------+ | id | name | +------+--------+ | 1 | Adam | | 2 | Adam | | 3 | Adam | | 4 | Adam | | 5 | Adam | | 6 | Adam | | 7 | Adam | | 8 | Serhat | | 9 | Winny | +------+--------+ 9 rows in set (0.00 sec)
- Related Articles
- Is it possible to make an insert or an update in the same MySQL query?
- What is the use of the LIMIT keyword in MySQL query?
- Is it possible to calculate a correlation in a MySQL query?
- Does MySQL update with regexp possible?
- MySQL query to update string field by concatenating to it?
- How to use MySQL CASE statement while using UPDATE Query?
- Does it make sense to use “LIMIT 1” in a query “SELECT 1 …”?
- How to update a column with the same values (not all) using UPDATE, SET and LIMIT in MySQL?
- How to use MySQL LIKE query to search a column value with % in it?
- Is it possible to cast in a MongoDB Query?
- Update two columns with a single MySQL query
- How to bulk update MySQL data with a single query?
- MySQL update query to remove spaces?
- MySQL query to update only month in date?
- Is it possible to use $(this) and universal selector (*) with jQuery?

Advertisements