How to increase or decrease value to another table?
How to increase or decrease value to another table?
I have two tables - bags and market.
At this time, in the bags table has data like :
If I add data from the market table, the qty column in the table bag will decrease according to the amount inputted in the market table. And the total price column will calculate the total_qty (market) * price (bags)
really? can you give me any solution how to solve this problem?
– Kusnadi
Aug 30 at 8:14
You need to provide sample data and desired results. Your explanation is not as clear as you think it is. By the way, the answer to your question is that you need a trigger.
– Gordon Linoff
Aug 30 at 11:09
1 Answer
1
From My Understanding I suggest for this concept to use Trigger,
DELIMITER $$
CREATE TRIGGER trg_insert
AFTER INSERT ON market
begin
update bags set qty = ((select qty from bags where id=1)-(select
market.total_qty from market where
id=1) ))
,price = ((select price from basgs where id=1)-(select market.total_prc
from market where
market.id=1)))
where bags.id = 1;
END$$
DELIMITER $$
Now i am using with static values which you are provided,
Try this code.
it's error sir.. 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'begin update bags set qty = (bags.qty-(select market.total_qty from market whe' at line 4'
– Kusnadi
Aug 30 at 22:14
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Seems like you want to store values calculated from other columns? That's generally a bad idea, too much risk of data inconsistency. Consider views or computed columns instead. (Or manage using triggers.)
– jarlh
Aug 30 at 8:10