I need Assistance please
I have two tables (X) And (Y)
Table (X) have { ID , material_Name , Material_number )
Table (Y) have { ID , Material_Name , Total )
Both appear on data gridview.
So, From table (X) i select the material_name from combobox. then i write the number of material in the textbox. then i press add button to insert the value in the table(X).
So let we assume From table (X) i selected the material_name is ( Pen) and the Material_number was (10).Then after three days i inserted the same material name but the material_number was (20). So the total Pin= 30 for now.
So i want this result (30) appear to me on table (Y) in column (Total )on data Gridview. And it can increase or decrease according to the inserting Material_number from table (X)
As summary ,
In Table (X) :-
First day >>> inserted 10 pens. & 5 soft
Second day >> inserted 20 pens .
The total pens that i have it in right now 10+20= (30). And total soft that i have = (5).
In Table(Y):-
The result (30)pens should be appear to me in column (Material_number)in the row of pens.
And the result of soft (5)should be appear to me in the column ( Material_number ) in the row of Soft
.
And like this for each material.
How can i do this?
An insert trigger will be run just before inserting into x.
CREATE TRIGGER insert_to_y BEFORE INSERT ON x
FOR EACH ROW
BEGIN
UPDATE Y SET total = total + NEW.material_number
WHERE Material_Name = NEW.Material_Name ;
END;
This way, when you run sql like this:
insert into x(material_name, material_number) values('soft', 5)
It will automatically update y
to add the change.
Note that my example only handles updates. If the value soft
doesnt exist in the Y
table, nothing will be updated. But the trigger can be adjusted to handle that.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.