当有人单击图像时,我会增加一个字段。我还想在计数器值达到1k时更新(减少)客户预算。就像这样:

UPDATE click SET count = count + 1 WHERE customer_id = 1;
UPDATE budget JOIN click ON budget.customer_id = click.customer_id
 SET budget.total_budget = budget.total_budget - 10, click.count = 0
 WHERE budget.customer_id = 1 AND click.count = 1000;


但这应该是线程安全的,当两次单击同时发生时,我不想减少预算2倍。我的问题是如何使用mysql做到这一点?

最佳答案

添加trigger,当budget超过某个值时,该更新count

10-07 15:12