gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:30:17 25 4
gpt4 key购买 nike

我在名为“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)

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

在表 employee 上插入新行时,我需要将表 tools 中的行插入表 employee_tools。

例如,如果我向员工值插入一个新行作为 ('1','Michel','Network')然后触发器应该从表工具中找到除法的工具名称并将行添加到 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)

状态将像下面这样手动更新...

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

最佳答案

就这么简单:

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;

关于mysql - 查找行并将其插入到另一个表 mysql 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15875953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com