This one's a bit too tricky for my SQL-foo.
这对我的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.
我需要做的是插入一个新行(包含3列),除非已经存在一行,其中两个值相同,如果它确实存在,我需要增加该行的第三个值。
Imagine 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).
想象一下,两个值是关系(a,b),第三个值是关系的强度(随着该关系的每一次发生而增加)。
Thanks in advance!
提前致谢!
1 个解决方案
#1
12
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
确保表上有一个UNIQUE(a,b)或PRIMARY KEY(a,b)约束
#1
12
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
确保表上有一个UNIQUE(a,b)或PRIMARY KEY(a,b)约束