mySQL - updating a qty field from adding and subtracting fields and capture timestamps? How?
I have one table:
I first create a new record in CARTONS_CURRENT inserting values to "part_no" and "qty".
Next, I later need to add or pull (subtract) from "qty" using two other columns for adding and pulling.
How do I make "qty" show the latest result from CARTONS_ADDED and CARTONS_PULLED?
I also need to timestamp each new, add and pull.
TABLE NAME: cartons_current
+------------+--------------+--------+--------+-------------------+------------+
| Column | Type 开发者_如何转开发 | Null | Key | Default | Extra |
+------------+--------------+--------+--------+-------------------+------------+
| part_no | varchar(20) | No | Prim | | |
+------------+--------------+--------+--------+-------------------+------------+
| qty | int(8) | No | | | |
+------------+--------------+--------+--------+-------------------+------------+
| qty_time | timestamp | No | | CURRENT_TIMESTAMP | |
+------------+--------------+--------+--------+-------------------+------------+
| add_qty | int(8) | No | | | |
+------------+--------------+--------+--------+-------------------+------------+
| add_time | timestamp | No | | CURRENT_TIMESTAMP | |
+------------+--------------+--------+--------+-------------------+------------+
| pull_qty | int(8) | No | | | |
+------------+--------------+--------+--------+-------------------+------------+
| pull_time | timestamp | No | | CURRENT_TIMESTAMP | |
+------------+--------------+--------+--------+-------------------+------------+
In a BEFORE UPDATE
trigger you can change the values, so you can do your timestamp magic in those, you you store the last change.
DELIMITER $$
CREATE TRIGGER ai_cartons_each BEFORE UPDATE ON cartons
FOR EACH ROW
BEGIN
IF new.qty_added <> old.qty_added THEN
SET new.added_timestamp = NOW();
END IF;
END $$
DELIMITER ;
See also this answer to your other question:
How do I update a qty field from adding and subtracting fields with timestamps?
精彩评论