繁体   English   中英

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

[英]inserting another row in mysql database with values similar to previous row

我有一张桌子,

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')

给出一个错误。

PS我知道上面不是一个好的数据库设计。 这是我实际设计的简化版本。

您可以使用INSERT ... SELECT

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

免责声明:我尚未对此进行测试,因此请您自担风险!

这行得通吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM