简体   繁体   English

查找并将行插入到另一个表中的mysql触发器

[英]find and insert row to another table mysql trigger

I have the following three tables in mysql database named "My_Company" 我在名为“ My_Company”的mysql数据库中有以下三个表

mysql> desc employee;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| Id       | int(11)     | NO   | PRI | 0       |       |
| Emp_Name | varchar(20) | YES  |     | NULL    |       |
| Division | varchar(20) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> desc tools;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Division  | varchar(20) | NO   | PRI |         |       |
| Tool_No   | int(11)     | NO   | PRI | 0       |       |
| Tool_Name | varchar(20) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

mysql> desc employee_tools;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| Id      | int(11)     | YES  |     | NULL    |       |
| Tool    | varchar(20) | YES  |     | NULL    |       |
| Status  | varchar(20) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

-------------------------------------------------------------------------------------

I need to insert the rows from table tools to table employee_tools when insert a new row on table employee. 在表employee上插入新行时,我需要将表工具中的行插入到表employee_tools中。

Example, if i insert a new row to employees values as ('1','Michel','Network') then the the trigger should to find the tool_names of division from table tools and add the rows to employee_tools 例如,如果我以('1','Michel','Network')的形式向员工值插入新行,则触发器应从表工具中查找除法的tool_names并将行添加到employee_tools

mysql> insert into employee values('1','Michel','Network');
Query OK, 1 row affected (0.05 sec)

mysql> select * from employee;
+----+----------+----------+
| Id | Emp_Name | Division |
+----+----------+----------+
|  1 | Michel   | Network  |
+----+----------+----------+
1 row in set (0.00 sec)

mysql> select * from tools;
+----------+---------+--------------+
| Division | Tool_No | Tool_Name    |
+----------+---------+--------------+
| Network  |       1 | Crimper      |
| Network  |       2 | LAN Tester   |
| Network  |       3 | Sleaver      |
| Hardware |       1 | Screw drv    |
| Hardware |       2 | Power Tester |
| Hardware |       3 | Plyer        |
+----------+---------+--------------+
3 rows in set (0.00 sec)

mysql> select * from employee_tools;
+------+------------+------------+
| Id   | Tool       |Status      |
+------+------------+------------+
|    1 | Crimper    |Working     |
|    1 | LAN Tester |working     |
|    1 | Sleaver    |working     |
+------+------------+------------+
3 rows in set (0.00 sec)

the status will be updated manually like below... 状态将手动更新,如下所示...

+------+------------+------------+
| Id   | Tool       |Status      |
+------+------------+------------+
|    1 | Crimper    |Working     |
|    1 | LAN Tester |Not working |
|    1 | Sleaver    |Broken      |
+------+------------+------------+

Simple as that: 就那么简单:

DROP TRIGGER IF EXISTS trg_emp_tools;
CREATE TRIGGER trg_emp_tools AFTER INSERT ON employee
FOR EACH ROW 
BEGIN
INSERT INTO employee_tools (Id, Tool, Status)
SELECT NEW.Id, tools.Tool_Name, 'Working'
FROM
tools
WHERE Division = NEW.Division;
END;

Those people uses phpmyadmin, you need to add delimiter in your trigger: 这些人使用phpmyadmin,您需要在触发器中添加定界符:

delimiter $$
DROP TRIGGER IF EXISTS copy_trig;
CREATE TRIGGER copy_trig AFTER INSERT ON test
FOR EACH ROW 
BEGIN
 insert into test_archive
 select * from test where id = New.id;
END$$
delimiter ;

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

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