如何使用相同的查询插入和更新多行到Sql数据库?

时间:2022-12-02 17:18:38

I have counter for unique column in my table. Now I have list where I have column name (which is unique) and counter (which contains some integer value). I know that it is possible to insert multiple lines, I just could make query containing all those items on the list, but problem is that if there is already data in unique field.

我的表中有唯一列的计数器。现在我有列表,其中列名称(唯一)和计数器(包含一些整数值)。我知道可以插入多行,我只是可以查询包含列表中的所有项目,但问题是如果已经有唯一字段中的数据。

In that case there is already this name and it has counter value, in this case I need to sum existing counter value to added alue and update that row. Is there any way to make all this with one Sql query? I use MySQL database and Java.

在这种情况下,已经存在此名称并且它具有计数器值,在这种情况下,我需要将现有计数器值与已添加的值相加并更新该行。有没有办法用一个Sql查询来完成所有这些?我使用MySQL数据库和Java。

1 个解决方案

#1


INSERT ON DUPLICATE KEY UPDATE

INSERT ON DUPLICATE KEY UPDATE

is the syntax you're looking for.

是你正在寻找的语法。

See here.

#1


INSERT ON DUPLICATE KEY UPDATE

INSERT ON DUPLICATE KEY UPDATE

is the syntax you're looking for.

是你正在寻找的语法。

See here.