Java Coder
Java Coder

Reputation:

How to insert and update multiple rows to Sql database with same query?

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.

Upvotes: 1

Views: 2872

Answers (1)

Carl Manaster
Carl Manaster

Reputation: 40336

INSERT ON DUPLICATE KEY UPDATE

is the syntax you're looking for.

See here.

Upvotes: 1

Related Questions