I’m inserting records from one table into another table using a select statement. The table being inserted to has a new field that should be incremented by one in each update, but should not be an auto-increment field as the number needs to start again for each group of records per update. If the select statement being used selects 42 records, the new table would have a field that is incremented by 1 for each record starting with one. The next group should have this field at 1 again and then increment for each record that was selected. Is this possible to do? I’m having trouble finding the syntax to use as most of my searching just finds results about auto-increment keys.
INSERT into images (filename, imgpath, imagenumber)
SELECT filename, imgpath, 1+
FROM old_images
WHERE event_id = 20
This is a simple example of the tables, but I’m wondering if there is a way to increment what is being put into imagenumber field in the images table directly in mySql.
Have a look at using User-Defined Variables
Something along the lines of