I have a problem, for example in my system I have the next table:
CREATE TABLE `sales` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` FLOAT NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
-- is more complex table
With content:
+-----+-------+
| id | amount|
+-----+-------+
|2023 | 100 |
|2024 | 223 |
|2025 | 203 |
|... |
|2505 | 324 |
+-----+-------+
I don't know the current id(There are sales every day). I'm trying to normalize the table.
UPDATE sales SET id=id - 2022;
Result:
+-----+-------+
| id | amount|
+-----+-------+
| 1 | 100 |
| 2 | 223 |
| 3 | 203 |
|... |
| 482 | 324 |
+-----+-------+
The problem
My problem was trying to change the AUTO_INCREMENT
, f.e.:
ALTER TABLE sales AUTO_INCREMENT = 483;
Its correct but I don't know the current id :(, I try the following query:
ALTER TABLE sales AUTO_INCREMENT = (SELECT MAX(id) FROM sales );
This causes me a error(#1064). Reading the documentation tells me:
In MySQL, you cannot modify a table and select from the same table in a subquery.
I try whit variables:
SET @new_index = (SELECT MAX(id) FROM sales );
ALTER TABLE sales AUTO_INCREMENT = @new_index;
But, this causes a error :(.