Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - UPDATE query with LIMIT

I want to update rows in my table with starting from 1001 to next 1000.

I tried with following query:

UPDATE `oltp_db`.`users` SET p_id = 3 LIMIT 1001, 1000 
  1. This is giving me syntax error. Is this correct? am I doing any mistake here.
  2. Can we limit update in this way?

Also, the rows that I am trying to update are having Null value for the column p_id which is having data type INTEGER. Due to this I am not even able to update using following query:

UPDATE `oltp_db`.`users` SET p_id = 3 WHERE p_id = null 
  1. Is my above query correct?
  2. What can be done to achieve this?
like image 636
Rahul Shelke Avatar asked Jun 09 '11 07:06

Rahul Shelke


People also ask

Can we use limit in UPDATE query MySQL?

Yes, it is possible to use UPDATE query with LIMIT in MySQL.

Which clause is used to limit the rows to be changed in an UPDATE statement?

The LIMIT clause places a limit on the number of rows that can be updated. For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions.


1 Answers

If you want to update multiple rows using limit in MySQL you can use this construct:

UPDATE table_name SET name='test' WHERE id IN (     SELECT id FROM (         SELECT id FROM table_name          ORDER BY id ASC           LIMIT 0, 10     ) tmp ) 
like image 149
Roopchand Avatar answered Oct 01 '22 11:10

Roopchand