Enlarging a column if a row already exists with the same values

This is too complicated for my SQL-foo.

What I need to do is insert a new row (which contains 3 columns), unless the row already exists, where two of these values ​​are the same, and if it exists, I need to increase the third value in this row.

Imagine that two values ​​are the ratio (a, b), and the third value is the strength of the relationship (which increases with each occurrence in this dependence).

Thanks in advance!

+7
source share
1 answer
INSERT INTO a_b (a, b, strength) VALUES ($a, $b, 1) ON DUPLICATE KEY UPDATE strength = strength + 1 

Make sure you have a UNIQUE (a, b) or PRIMARY KEY (a, b) in the table

+12
source

All Articles