How do I update the quantity column from two other tables in mySQL?
I have three tables so I can display separate tables and keep track of different things.
How do I make 'pull_qty" in CARTONS_PULLED and 'add_qty' in CARTONS_ADDED update 'qty' in CARTONS _CURRENT?
When I create a new Part Number with a quantity it inserts into CARTONS_CURRENT.
Now I need to update the 'qty" in CARTONS_CURRENT with adds and pulls from the two other tables? the 'part_no' is the primary key and is always the reference to update.
Here are my tables:
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 | | | 开发者_Go百科 |
| 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 | | | |
+--------------+--------------+--------+--------+-------------------+------------+
You'd write triggers for the CARTONS_PULLED
and CARTONS_ADDED
tables which do the appropriate updates in CARTONS_CURRENT
table. Ie something like
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;
if you want to add the value of add_qty
to the CARTONS_CURRENT.qty
when new record is inserted into CARTONS_ADDED
.
精彩评论