我有三张桌子,所以我可以显示不同的表格并跟踪不同的事情。
如何在CARTONS_PULLED中制作'pull_qty',在CARTONS_CURRENT中的CARTONS_ADDED更新'qty'中制作'add_qty'?
当我使用数量创建新的零件编号时,它会插入到CARTONS_CURRENT。
现在我需要更新CARTONS_CURRENT中的'qty'并添加和拉取其他两个表吗?'part_no'是主键,并且始终是更新的引用。
以下是我的表格:
DATABASE NAME: _hero
TABLE NAME: CARTONS_CURRENT
+--------------+--------------+--------+--------+-------------------+------------+
| Column | Type | Null | Key | Default | Extra |
+--------------+--------------+--------+--------+-------------------+------------+
| orig_time | timestamp | No | | CURRENT_TIMESTAMP | |
| type | text | No | | | |
| part_no | varchar(20) | No | Prim | | |
| description | varchar(75) | No | | | |
| count | varchar(2) | No | | | |
| size | varchar(30) | No | | | |
| min | int(7) | No | | | |
| max | int(7) | No | | | |
| qty | int(8) | No | | | |
+--------------+--------------+--------+--------+-------------------+------------+
TABLE NAME: CARTONS_ADDED
+--------------+--------------+--------+--------+-------------------+------------+
| Column | Type | Null | Key | Default | Extra |
+--------------+--------------+--------+--------+-------------------+------------+
| add_time | timestamp | No | Prim | CURRENT_TIMESTAMP | |
| type | text | No | | | |
| part_no | varchar(20) | No | Prim | | |
| add_type | varchar(25) | No | | | |
| add_qty | int(8) | No | | | |
| add_ref | varchar(35) | No | | | |
| add_by | text | No | | | |
| add_notes | varchar(300) | No | | | |
+--------------+--------------+--------+--------+-------------------+------------+
TABLE NAME: CARTONS_PULLED
+--------------+--------------+--------+--------+-------------------+------------+
| Column | Type | Null | Key | Default | Extra |
+--------------+--------------+--------+--------+-------------------+------------+
| pull_time | timestamp | No | Prim | CURRENT_TIMESTAMP | |
| type | text | No | | | |
| part_no | varchar(20) | No | Prim | | |
| pull_type | varchar(25) | No | | | |
| pull_qty | int(8) | No | | | |
| pull_ref | varchar(35) | No | | | |
| pull_by | text | No | | | |
| pull_notes | varchar(300) | No | | | |
+--------------+--------------+--------+--------+-------------------+------------+
答案 0 :(得分:0)
您为CARTONS_PULLED
和CARTONS_ADDED
表编写triggers,在CARTONS_CURRENT
表中执行相应的更新。就像
CREATE TRIGGER Upd_Cartons_qty
AFTER INSERT ON CARTONS_ADDED FOR EACH ROW
BEGIN
UPDATE CARTONS_CURRENT SET qty = qty + NEW.add_qty WHERE part_no = NEW.part_no;
END;
如果要在add_qty
中插入新记录时将CARTONS_CURRENT.qty
的值添加到CARTONS_ADDED
。