开发者

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

开发者 https://www.devze.com 2023-02-20 12:37 出处:网络
This one\'s a bit too tricky for my SQL-foo. What I need to do is insert a new row (which contains 3 columns), unless a row already exists where two of those values are the same, and if it does exist

This one's a bit too tricky for my SQL-foo.

What I need to do is insert a new row (which contains 3 columns), unless a row already exists where two of those values are the same, and if it does exist, I need to increment the third value on that row.

Im开发者_如何学Goagine the two values being a relationship (a, b) and the third value being the relationships's strength (which increases with every ocurrence of that relationship).

Thanks in advance!


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) constraint on the table

0

精彩评论

暂无评论...
验证码 换一张
取 消