简体   繁体   English

插入后使用来自另一个表的数据触发更新同一表

[英]Trigger update the same table with data from another table after insert

I have two unrelated tables tbl_A & tbl_B 我有两个不相关的表tbl_A和tbl_B

tbl_A tbl_A

+----+---------------------+------+
| id | url                 | slug |
+----+---------------------+------+
|  1 | http://example.com/ | 3qqd |           
|  2 | http://example.com/ | t8af |           
|  3 | http://example.com/ | sjim |           
|  4 | http://example.com/ | awfo |           
|  5 | http://example.com/ | 6myy |           
+----+---------------------+------+

tbl_A description: tbl_A说明:

+---------------------+---------------------+------+-----+---------------------+----------------+
| Field               | Type                | Null | Key | Default             | Extra          |
+---------------------+---------------------+------+-----+---------------------+----------------+
| id                  | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment |
| url                 | text                | YES  |     | NULL                |                |
| slug                | varchar(255)        | YES  | MUL | NULL                |                |
+---------------------+---------------------+------+-----+---------------------+----------------+

and another table : 和另一个表:

tbl_B tbl_B

+----+---------------------+---------------------+------+           
| ID | user_name           | url                 | slug |
+----+---------------------+---------------------+------+
|  1 | john.reese          | NULL                | NULL |       
+----+---------------------+---------------------+------+

tbl_B description : tbl_B说明:

+---------------------+---------------------+------+-----+---------------------+----------------+
| Field               | Type                | Null | Key | Default             | Extra          |
+---------------------+---------------------+------+-----+---------------------+----------------+
| ID                  | bigint(20) unsigned | NO   | PRI | NULL                | auto_increment |
| user_name           | varchar(60)         | NO   | MUL |                     |                |
| url                 | text                | YES  |     | NULL                |                |
| slug                | varchar(255)        | YES  | MUL | NULL                |                |
+---------------------+---------------------+------+-----+---------------------+----------------+

tbl_A.id is unrelated to tbl_B.ID. tbl_A.id与tbl_B.ID无关。

tbl_B.ID is userID field and gets filled up dnamically when a new user registers. tbl_B.ID是userID字段,当新用户注册时会被动态填充。 So that tbl_B.ID gets a row inserted automatically as a user register, tbl_B.ID value gets auto incremented. 为了使tbl_B.ID获得作为用户寄存器自动插入的行,tbl_B.ID值将得到自动递增。

tbl_A on the other hand already exists with all the details. 另一方面,tbl_A已包含所有详细信息。

What I want to achieve: whenever a new user registers and userID is INSERT into tbl_B.ID, at the same time it should trigger an update of tbl_B.user and tbl_B.slug with the values taken from tbl_A.user and tbl_A.slug. 我要实现的目标是:每当新用户注册并且将userID插入到tbl_B.ID中时,同时它应该使用从tbl_A.user和tbl_A.slug中获取的值触发tbl_B.user和tbl_B.slug的更新。

Outcome : After ID 1 is added 结果 :添加ID 1之后

+----+---------------------+---------------------+------+           
| ID | user_name           | url                 | slug |
+----+---------------------+---------------------+------+
|  1 | john.reese          | http://example.com/ | 3qqd |       
+----+---------------------+---------------------+------+

Hope I am able to explain. 希望我能解释。 I was trying to use triggers but got lost, am a newbie with mysql, please bear with me. 我试图使用触发器,但迷路了,是mysql的新手,请耐心等待。

drop trigger if exists bi_tbl_B $$
delimiter $$

create trigger bi_tbl_B before insert on tbl_B
for each row begin
   UPDATE tbl_B
     SET url = url +
       (SELECT url
          FROM tbl_A
          WHERE id = NEW.id)
    WHERE ID = NEW.ID;

end;
$$

delimiter ; 

I don't know whether this is even possible or should I try the other way round. 我不知道这是否有可能,或者我应该反过来尝试。

Add a field user_id in tbl_A and AFTER INSERT on tbl_B update tabl_A.user_id column with the userID from tbl_B.ID 在tbl_A中添加字段user_id ,然后在tbl_B上添加AFTER INSERT ,使用来自tbl_B.ID的userID更新tabl_A.user_id列

I am open to suggestions, if not trigger then procedures. 我愿意接受建议,如果没有触发的话,请联系我们。

It is possible to do it, just not the way you are trying. 可以做到这一点,而不仅仅是您尝试的方式。 In the before insert trigger you can change the values being inserted by changing the NEW.field_name variables. 在插入之前触发器中,您可以通过更改NEW.field_name变量来更改要插入的值。

drop trigger if exists bi_tbl_B $$
delimiter $$

create trigger bi_tbl_B before insert on tbl_B
for each row begin
   DECLARE v_slug as varchar(255);
   DECLARE v_url as text;
   SELECT url, slug INTO v_url, v_slug FROM tbl_A WHERE id = NEW.id;
   NEW.url=v_url;
   NEW.slug=v_slug;
end;
$$

delimiter ; 

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

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