在插入

时间:2016-10-02 22:41:20

标签: mysql sql triggers

我有两个不相关的表tbl_A& tbl_B

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说明:

+---------------------+---------------------+------+-----+---------------------+----------------+
| 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                |                |
+---------------------+---------------------+------+-----+---------------------+----------------+

和另一张表:

tbl_B

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

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与tbl_B.ID无关。

tbl_B.ID是userID字段,当新用户注册时,会以dnamically方式填充。这样tbl_B.ID会自动将一行插入用户注册表,tbl_B.ID值会自动递增。

另一方面,

tbl_A已经存在所有细节。

  

我想要实现的目标:每当新用户注册并且userID是   INSERT到tbl_B.ID,同时它应该触发更新   tbl_B.user和tbl_B.slug,其值取自tbl_A.user和   tbl_A.slug。

结果:添加ID 1后

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

希望我能解释一下。我试图使用触发器但迷路了,我是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 ; 

我不知道这是否可能,或者我应该反过来试试。

在tbl_A中添加字段user_id,在tbl_B更新tabl_A.user_id列中添加AFTER INSERT,其中包含来自tbl_B.ID的用户ID

我愿意接受建议,如果没有触发那么程序。

1 个答案:

答案 0 :(得分:1)

可以这样做,而不是你想要的方式。在before insert触发器中,您可以通过更改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 ;