Skip to content
Advertisement

How to make a copied data, then insert back and update the values in one column, id will be auto-increment in the same table?

Table Car

id  vin   car_name   mileage
1   1GH   HONDA      10,000
2   2LM   HONDA      10,000
3   3QW   HONDA      10,000
4   4RT   HONDA      10,000
5   5TY   HONDA      10,000
6   6UI   HONDA      10,000
7   7OP   HONDA      10,000
8   8BN   HONDA      10,000
9   9AS   HONDA      10,000
10  10GM  HONDA      10,000

Table Car

id  vin   car_name   mileage
1   1GH   HONDA      10,000
2   2LM   HONDA      10,000
3   3QW   HONDA      10,000
4   4RT   HONDA      10,000
5   5TY   HONDA      10,000
6   6UI   HONDA      10,000
7   7OP   HONDA      10,000
8   8BN   HONDA      10,000
9   9AS   HONDA      10,000
10  10GM  HONDA      10,000
1   1GH   HONDA      10,000
2   2LM   HONDA      10,000
3   3QW   HONDA      10,000
4   4RT   HONDA      10,000
5   5TY   HONDA      10,000

Copy the first 5 data, insert back and set new VIN values for copied data, id will be auto-increment

Advertisement

Answer

If id is an identity or auto increment column (or something comparable), you can use:

insert into car (vin, car_name, mileage)
select vin, car_name, mileage
from car 
where id between 1 and 5;

Not sure what “update the VIN” is supposed to be though.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement