[英]inserting another row in mysql database with values similar to previous row
I have a table like, 我有一张桌子,
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
I am trying to add another batchid 3 with same volume1, volume2 and volume3 values such that it looks like (same records from batchID 2) 我正在尝试添加另一个具有相同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
I wrote the following query 我写了以下查询
insert into table1 (BatchID,Volume1,Volume2,Volume3) Values
(`3`,Select Volume1,Volume2,Volume3 from table1 where batchid = '2')
gives an error. 给出一个错误。
PS I know the above is not a good database design. PS我知道上面不是一个好的数据库设计。 This is over simplified version of my actual design.
这是我实际设计的简化版本。
You can use INSERT ... SELECT
: 您可以使用
INSERT ... SELECT
:
INSERT INTO table1 (BatchID, Volume1, Volume2, Volume3)
SELECT 3, Volume1, Volume2, Volume3 FROM table1 WHERE batchid = 2
DISCLAIMER: I haven't tested this, so try it at your own risk! 免责声明:我尚未对此进行测试,因此请您自担风险!
Would this work? 这行得通吗?
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.