在mysql数据库中插入另一行,其值与上一行类似

时间:2012-06-05 00:55:41

标签: mysql insert

我有一张像

这样的表
BatchID Volume1 Volume2 Volume3

1        3.0      4.0     5.0
1        2.0      1.0     2.0
2        1.0      2.0     4.0 
2        1.0      1.0     1.0
2        1.0      1.0     1.0

我正在尝试添加另一个具有相同volume1,volume2和volume3值的batchid 3 它看起来像(来自batchID 2的相同记录)

1        3.0      4.0     5.0
1        2.0      1.0     2.0
2        1.0      2.0     4.0 
2        1.0      1.0     1.0
2        1.0      1.0     1.0
3        1.0      2.0     4.0 
3        1.0      1.0     1.0
3        1.0      1.0     1.0

我写了以下查询

insert into table1 (BatchID,Volume1,Volume2,Volume3) Values 
(`3`,Select Volume1,Volume2,Volume3 from table1 where batchid = '2')

给出错误。

P.S我知道上面的数据库设计不是很好。这是我实际设计的简化版本。

2 个答案:

答案 0 :(得分:3)

您可以使用INSERT ... SELECT

INSERT INTO table1 (BatchID, Volume1, Volume2, Volume3)
SELECT 3, Volume1, Volume2, Volume3 FROM table1 WHERE batchid = 2

答案 1 :(得分:1)

免责声明:我没有对此进行测试,因此请自行承担风险!

这会有用吗?

INSERT INTO table1 (BatchID, Volume1, Volume2, Volume3)
    SELECT 3, Volume1, Volume2, Volume3
    FROM table1
    WHERE BatchID = 2;
相关问题