105

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?
Rahul Shelke
  • 2,052
  • 4
  • 28
  • 50

10 Answers10

175

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
)
Kijewski
  • 25,517
  • 12
  • 101
  • 143
Roopchand
  • 2,608
  • 2
  • 17
  • 20
  • 33
    MySQL 5.5 does not support LIMIT in IN/ALL/ANY/SOME subquery: Error Code 1235 – Philipp Feb 11 '14 at 18:34
  • 13
    @FiveO Works fine for me on MySQL 5.5.34. Without the nested subquery (the perverse `SELECT id FROM (SELECT id FROM ...)` construct) I get `ERROR 1235 (42000): This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'`, but if I include both selects as shown here, the query works. – Mark Amery Jul 06 '14 at 21:27
  • 3
    By the way, for Postgres 9.4, this kind of code works without needing the outer `SELECT id FROM (` … `) tmp `. Thanks. Seems like this should be the Accepted Answer. – Basil Bourque Sep 16 '15 at 21:54
  • Yep, limit doesn't work with update. +1 for you, man :) – aligatorr89 Feb 18 '21 at 18:18
32

When dealing with null, = does not match the null values. You can use IS NULL or IS NOT NULL

UPDATE `smartmeter_usage`.`users_reporting` 
SET panel_id = 3 WHERE panel_id IS NULL

LIMIT can be used with UPDATE but with the row count only

David
  • 3,285
  • 1
  • 37
  • 54
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
17

In addition to the nested approach above, you can accomplish the application of theLIMIT using JOIN on the same table:

UPDATE `table_name`
INNER JOIN (SELECT `id` from `table_name` order by `id` limit 0,100) as t2 using (`id`)
SET `name` = 'test'

In my experience the mysql query optimizer is happier with this structure.

Jerry
  • 3,391
  • 1
  • 19
  • 28
12

I would suggest a two step query

I'm assuming you have an autoincrementing primary key because you say your PK is (max+1) which sounds like the definition of an autioincrementing key.
I'm calling the PK id, substitute with whatever your PK is called.

1 - figure out the primary key number for column 1000.

SELECT @id:= id FROM smartmeter_usage LIMIT 1 OFFSET 1000

2 - update the table.

UPDATE smartmeter_usage.users_reporting SET panel_id = 3 
WHERE panel_id IS NULL AND id >= @id 
ORDER BY id 
LIMIT 1000

Please test to see if I didn't make an off-by-one error; you may need to add or subtract 1 somewhere.

Johan
  • 74,508
  • 24
  • 191
  • 319
4

You can do it with a LIMIT, just not with a LIMIT and an OFFSET.

Paul V
  • 351
  • 3
  • 9
4
UPDATE `smartmeter_usage`.`users_reporting` SET panel_id = 3 LIMIT 1001, 1000

This query is not correct (or at least i don't know a possible way to use limit in UPDATE queries), you should put a where condition on you primary key (this assumes you have an auto_increment column as your primary key, if not provide more details):

UPDATE `smartmeter_usage`.`users_reporting` SET panel_id = 3 WHERE primary_key BETWEEN 1001 AND 2000

For the second query you must use IS

UPDATE `smartmeter_usage`.`users_reporting` SET panel_id = 3 WHERE panel_id is null

EDIT - if your primary_key is a column named MAX+1 you query should be (with backticks as stated correctly in the comment):

UPDATE `smartmeter_usage`.`users_reporting` SET panel_id = 3 WHERE `MAX+1` BETWEEN 1001 AND 2000

To update the rows with MAX+1 from 1001 TO 2000 (including 1001 and 2000)

Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • 2
    You make the assumption that he has an auto_increment on the table, which might not be the case, nor does it have to be an unbroken series of numbers. – jishi Jun 09 '11 at 08:02
  • well, yes, of course i'm making that assumption, i'll update the answer with that, waiting for more information! :) – Nicola Peluchetti Jun 09 '11 at 08:04
  • Thank you for response. Actually the primary key is MAX+1. – Rahul Shelke Jun 09 '11 at 08:34
  • 1
    -1, (A) max is a reserved word, needs to be quoted in backticks. (B) `max + 1 between 1001 and 2000` should be rewritten to `max between 1000 and 1999`. (C) I've never heard of a primary key called `max+1` that makes no sense. (D) This code assumes column `max` is continous, which is not guaranteed by any means. (E) limit does work with `update`. – Johan Jun 09 '11 at 09:20
  • Great answer. Your first UPDATE query works perfect if you have a PK auto increment! – devasia2112 Feb 07 '12 at 15:32
1

You should use IS rather than = for comparing to NULL.

UPDATE `smartmeter_usage`.`users_reporting`
SET panel_id = 3
WHERE panel_id IS null

The LIMIT clause in MySQL when applied to an update does not permit an offset to be specified.

Will A
  • 24,780
  • 5
  • 50
  • 61
  • This helped me in a similar but different situation - query should have read "update my_table set process_id = n limit 1000" instead of "update my_table set process_id = n limit 0,1000" – rrrhys Sep 29 '13 at 23:04
0

For people get this post by search "update limit MySQL" trying to avoid turning off the safe update mode when facing update with the multiple-table syntax.

Since the offical document state

For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

https://stackoverflow.com/a/28316067/1278112
I think this answer is quite helpful. It gives an example

UPDATE customers SET countryCode = 'USA' WHERE country = 'USA'; -- which gives the error, you just write:

UPDATE customers SET countryCode = 'USA' WHERE (country = 'USA' AND customerNumber <> 0); -- Because customerNumber is a primary key you got no error 1175 any more.

What I want but would raise error code 1175.

UPDATE table1 t1
        INNER JOIN
    table2 t2 ON t1.name = t2.name 
SET 
    t1.column = t2.column
WHERE
    t1.name = t2.name;

The working edition

UPDATE table1 t1
        INNER JOIN
    table2 t2 ON t1.name = t2.name 
SET 
    t1.column = t2.column
WHERE
    (t1.name = t2.name and t1.prime_key !=0);

Which is really simple and elegant. Since the original answer doesn't get too much attention (votes), I post more explanation. Hope this can help others.

Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57
0

You should highly consider using an ORDER BY if you intend to LIMIT your UPDATE, because otherwise it will update in the ordering of the table, which might not be correct.

But as Will A said, it only allows limit on row_count, not offset.

jishi
  • 24,126
  • 6
  • 49
  • 75
0

The limiting column name is not given but assuming it to be XX, then something like this should do. It is tested on MariaDB 10:

UPDATE `oltp_db`.`users` SET `p_id` = '3' WHERE `p_id` IS NULL AND `XX` > 1000 LIMIT 1000;

This will target the right records. The only unknown side-effect is that although the affected rows show the limit number, the actual changes may be less.

Ajowi
  • 449
  • 3
  • 12