在mysql数据库中插入另一行,其值与上一行类似(inserting another row in mysql database with values similar to previous row

编程入门 行业动态 更新时间:2024-10-24 19:16:39
在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.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)

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.

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 = 2

You can use INSERT ... SELECT:

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

更多推荐

本文发布于:2023-07-05 01:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031257.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库中   类似   inserting   mysql   row

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!