How record with the next number in the order added to the table?

Viewed 19

For example table:

+----+------+------+
| id | name | price| 
+----+------+------+
| 4  | ABC  | 1000 | 
| 5  | ABD  | 1001 | 
+----+------+------+

How insert in table following lines?

+----+------+------+
| 6  | ABF  | 1002 | 
| 7  | ABG  | 1003 | 
| 8  | ABH  | 1004 | 
+----+------+------+

This script does not work correctly:

insert into table
(id, name, price) 
select max(id)+1, 'ABF', max(price)+1  from table
union all
select max(id)+1, 'ABG', max(price)+1  from table
union all
select max(id)+1, 'ABH', max(price)+1  from table
1 Answers

We don't know your database, but if id be an auto increment column, then you should not be passing a value for it. Try this version:

INSERT INTO yourTable (name, price)
VALUES
    ('ABF', 1002),
    ('ABG', 1003),
    ('ABH', 1004);
Related