例如表:
+----+------+------+
| id | name | price|
+----+------+------+
| 4 | ABC | 1000 |
| 5 | ABD | 1001 |
+----+------+------+
如何在表格中插入以下行?
+----+------+------+
| 6 | ABF | 1002 |
| 7 | ABG | 1003 |
| 8 | ABH | 1004 |
+----+------+------+
此脚本无法正常工作:
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
我们不知道你的数据库,但如果id
是一个自动递增列,那么你不应该为它传递值。试试这个版本:
INSERT INTO yourTable (name, price)
VALUES
('ABF', 1002),
('ABG', 1003),
('ABH', 1004);