在mysql数据库中插入另一行,其值与上一行类似(inserting another row in mysql database with values similar to previous row)
mysql
我有一张桌子,
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我知道上面的数据库设计并不好。 这是我实际设计的简化版本。
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.0I am trying to add another batchid 3 with same volume1, volume2 and volume3 values such that it looks like (same records from 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.0I 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.
P.S I know the above is not a good database design. This is over simplified version of my actual design.
最满意答案
你可以使用INSERT ... SELECT :
INSERT INTO table1 (BatchID, Volume1, Volume2, Volume3) SELECT 3, Volume1, Volume2, Volume3 FROM table1 WHERE batchid = 2You can use INSERT ... SELECT:
INSERT INTO table1 (BatchID, Volume1, Volume2, Volume3) SELECT 3, Volume1, Volume2, Volume3 FROM table1 WHERE batchid = 2更多推荐
发布评论