简体   繁体   English

MySQL触发器-在table1中插入时更新table2

[英]MySQL trigger - Update table2 on insert in table1

What I want to do is to add some credit (10% of user´s credit) to user´s account with each 3rd, 6th, 9th and 12th (not more) user added into database under this user´s invite link (but invite link isn´t essential here - each user has index column with ID of his invitator). 我想做的是向用户帐户中添加一些信用(占用户信用的10%),并将每个第3,第6,第9和第12个(不多)用户添加到该用户的邀请链接下的数据库中(但是邀请链接在这里不是必不可少的-每个用户都有其邀请者ID的索引列。

So something like this (sorry for "pseudocode", I never had to use the trigger and probably won´t have to any soon, so I have no idea how to write it properly): 所以像这样(对“伪代码”很抱歉,我从不需要使用触发器,而且可能很快就不需要了,所以我不知道如何正确编写它):

UPDATE accounts.credit = accounts.credit + (accounts.credit/10)
ON INSERT INTO users (AND when inserted row % 3 == 0 to some user)
WHERE k_user = this

Or is there any easier way how to do this? 还是有更简单的方法来做到这一点? I could handle PHP, but I think the script can execute only if user visits the site... 我可以处理PHP,但是我认为脚本只有在用户访问该网站时才能执行...

Consider using triggers https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html 考虑使用触发器https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

delimiter $$
CREATE TRIGGER upd_check AFTER INSERT ON users
       FOR EACH ROW
       BEGIN
           IF NEW.id % 3 = 0 THEN
               UPDATE accounts SET credit = credit + (credit / 10) where k_user = NEW.id
           END IF;
       END;$$
delimiter ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM