如果存在行,则更新(删除)值

时间:2018-06-10 17:41:32

标签: mysql

我有这样的表:

(0)
PRIMARY(id) UNIQUE(account, type)
id | account | type | amount
1    1         1      3
2    2         2      2
3    1         2      1

所以,(1)我需要减少一行的数量,同时增加另一行; (2)如果我们增加的行不存在,我们创建它的数量为1; (3)如果我们减少的行数是1,我们必须删除记录; (4)如果我们正在减少的行不存在,我们什么都不做。

示例:

(0->1) Decrease id 2, increase id 3 result:
id | account | type | amount
1    1         1      3
2    2         2      1
3    1         2      2

(0->2) Decrease id 1, increase account 2 with type 1 result:
id | account | type | amount
1    1         1      2
2    2         2      2
3    1         2      1
4    2         1      1

(0->3) Decreasing id=3, increasing id=2 result:
id | account | type | amount
1    1         1      3
2    2         2      3

(0->4) Decreasing account 3 with type 1, increasing id=1 result:
id | account | type | amount
1    1         1      3
2    2         2      2
3    1         2      1

(extra) Decreasing id=3, increasing account 3 result:
id | account | type | amount
1    1         1      3
2    2         2      2
3    3         2      1   

ID用于指向您更快,实际上我们关注对account_type;所以我们正在将某种类型的单位从一个帐户转移到另一个帐户;

如何在MySQL中实现此行为?

0 个答案:

没有答案
相关问题