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
- This is giving me syntax error. Is this correct? am I doing any mistake here.
- 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
- Is my above query correct?
- What can be done to achieve this?
If you want to update multiple rows using limit in MySQL you can use this construct:
You can do it with a LIMIT, just not with a LIMIT and an OFFSET.
When dealing with null,
=
does not match the null values. You can useIS NULL
orIS NOT NULL
LIMIT
can be used withUPDATE
but with therow count
onlyYou should use IS rather than = for comparing to NULL.
The
LIMIT
clause in MySQL when applied to an update does not permit an offset to be specified.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.
2 - update the table.
Please test to see if I didn't make an off-by-one error; you may need to add or subtract 1 somewhere.
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):For the second query you must use IS
EDIT - if your primary_key is a column named MAX+1 you query should be (with backticks as stated correctly in the comment):
To update the rows with MAX+1 from 1001 TO 2000 (including 1001 and 2000)