Skip to content
Advertisement

Change id values of all rows in table STARTING from a specific number

I have 1000 rows in a table..

I’m getting the last and next available id (to start from)

$latest_id = ("SELECT * FROM `vehicles` WHERE ID = (SELECT MAX(ID) FROM `vehicles`)");

What I’m trying to achieve is when clicking on a button, to change the id value of all 1000 rows + to start from the $latest_id

So NOW is:

id 1 = car 1
id 2 = car 2
id 3 = car 3
...

AFTER

id 1001 = car 1
id 1002 = car 2
id 1003 = car 3
...

Can someone help me with the sql query please.

Advertisement

Answer

Use an UPDATE statement with a JOIN

UPDATE vehicles as v
JOIN (SELECT MAX(id) AS maxid
      FROM vehicles) AS x
SET v.id = v.id + x.maxid
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement