如何根据前一行的值插入新行并增加列?

时间:2015-02-04 19:00:12

标签: mysql ms-access

表名:ORDERTABLE

ID (PK) || ORDER_ID || ITEMNAME || QTY
=======================================
1       ||    1     ||  apple   ||  2
2       ||    1     ||  orange  ||  5
3       ||    2     ||  pear    ||  1
4       ||    2     ||  grapes  ||  1
5       ||    2     ||  melon   ||  1

如何根据SQL中以前的ORDER_ID数插入新行?

所以我会得到这个:

ID (PK) || ORDER_ID || ITEMNAME || QTY
=======================================
1       ||    1     ||  apple   ||  2
2       ||    1     ||  orange  ||  5
3       ||    2     ||  pear    ||  1
4       ||    2     ||  grapes  ||  4
5       ||    2     ||  melon   ||  2
6       ||    3     ||  mango   ||  3

目前我的查询字符串是:

insert into ORDERTABLE(ORDER_ID,ITEMNAME,QTY) values (LAST()+1, 'mango',3)

只是为了得到这个错误:

wrong number of arguments used with function in query expression

1 个答案:

答案 0 :(得分:1)

INSERT INTO ORDERTABLE(ORDER_ID,ITEMNAME,QTY)SELECT MAX(ORDER_ID)+1,' mango',' 3'来自ORDERTABLE;