Mysql Trigger - Update table when insert another table - mysql

I have three tables:
Import Table:
import_id | import_date
-------------------------
1 | 2019/07/29
2 | 2019/07/28
ImportItem Table:
import_id | product_id | quantity
-------------------------------------
1 | 1 | 50
1 | 2 | 60
1 | 3 | 20
Product Table:
product_id | quantity
---------------------------
1 | 10
2 | 5
3 | 15
I want to create a trigger update quantity in product table when insert ImportItem table.
Any help much appreciated! Thanks very much!

If you create a trigger in your table import_item that will update your table product after each import_item INSERT you can do this :
CREATE TRIGGER trigger_name
AFTER INSERT
ON import_item FOR EACH ROW
BEGIN
UPDATE product SET quantity = NEW.quantity WHERE product_id = NEW.product_id;
END
Here is some documentation about the trigger key words NEW and OLD : documentation
When you are in a BEFORE/AFTER INSERT trigger, you can use NEW.field to get the value of the new field you just inserted.
Now if you are in a BEFORE/AFTER UPDATE trigger, you can use NEW.field to get the value of the field AFTER the update and OLD.value to get the value of the field BEFORE the update.
And the last, if you are in a BEFORE/AFTER DELETE trigger , you can use OLD.field to get the value fo the field before the delete.
Is it what you are looking for ?

Related

how to alter a table to combine two rows that are the same and add the qty column up

In MySQL I need to alter a table to combine 2 rows if they are the same and have it add the qty column together. I don't want to create a new table just alter the one that's there for example if I have a row that is:
Account
Name
Item
Qty
123
John
Red Shoe
1
123
John
Red Shoe
1
How would I alter the table to combine the two rows to be:
Account
Name
Item
Qty
123
John
Red Shoe
2
I cannot find the answer to this kind of question anywhere online.
It isn't clear what you are asking for. Alter Table is not appropriate and there is no mysql function which can combine rows or any statement which could insert new rows and delete other rows - you would need an insert followed by a delete..but the delete would have to know what to delete and it's not obvious from your question how this could be achieved
DROP table if exists t;
create table t
(Account int,Name varchar(20), Item varchar(20) ,Qty int);
insert into t values
(123 ,'John' ,'Red Shoe' ,1),
(123 ,'John' ,'Red Shoe' ,1);
#select * from t;
insert into t
select account,name,item,sum(qty)
from t
group by account,name,item;
select * from t;
---------+------+----------+------+
| Account | Name | Item | Qty |
+---------+------+----------+------+
| 123 | John | Red Shoe | 1 |
| 123 | John | Red Shoe | 1 |
| 123 | John | Red Shoe | 2 |
+---------+------+----------+------+
3 rows in set (0.001 sec)

Create Trigger : update table column by inserting data into another table rows

Please Help me to create triggers,
I have 2 tables of
data
+---------+------------+-------+-------+
| id | info_id | rate | qty | total |
+----+---------+------------+----------+
| 80 | 10 | 150 | 5 | 750 |
+----+---------+-------+-------+-------+
| 81 | 10 | 50 | 5 | 250 |
+--------------+-------+-------+-------+
info
+---------+---------------+------------+
| id | name | gtotal | dated |
+-----+--------+----------+------------+
| 10 | Hari | NULL | 2021-05-15 |
+---------+------------+---------------+
I want to create a trigger through phpmyadmin, as soon as data will be inserted, then info.gtotal will be updated by adding data.total from matched info_id from table name- data.
what will be the trigger if update on data happens too. I just want to create both triggers.
I am new with such, please help me. Any help is accepted.
You need to create two triggers one for insert and another for update.
CREATE TRIGGER `insert_trigger`
AFTER INSERT ON `data`
FOR EACH ROW
UPDATE info
SET gtotal = gtotal + new.total
WHERE id = new.info_id;
Here new.info_id will have the info_id value of newly insert record
CREATE TRIGGER `update_trigger`
AFTER UPDATE ON `data`
FOR EACH ROW
UPDATE info
SET gtotal = (gtotal - old.total) + new.total
WHERE id = new.info_id;
Here old.total will have the total value of record before updating. new.total is the total value after updation.
Since you haven't conveyed what to do after update I have added the logic of subtracting the total from old total and added the new total value. Change it as per your requirement.
You can also create these triggers using GUI in phpmyadmin. You have to select the triggers in the menu bar and add the trigger definition.

How to apply automated query after each insert with condition?

If I have a table like that:
id | name | points
------------------
1 | Hala | 50
2 | Asa | 60
3 | Hala | 1000
How can I apply automated query that applied after each insert but with condition
like this query but only to apply on the latest item after it is inserted
update points set points = points / 10 where name = 'Hala';
Try to create a trigger like this
CREATE TRIGGER MyTrigger AFTER INSERT ON MyTable
FOR EACH ROW BEGIN
SET NEW.points = NEW.points/10;
END

mysql trigger update a record when certain record value contained in new inserted record from other table

I'm new here and really need help. Work around it for days and haven't met the solution. I work with gammu.
I have these tables
table inbox
+--------------+
| TextDecoded |
+--------------+
|xx200.001,00xx|
|xx300.001,00xx|
+--------------+
table balance
+-------------+----------------+----------------+------------+
| costumer_id | refill_balance | refill_nominal | status |
+-------------+----------------+----------------+------------+
| 1 | 2000001 | 300.001,00 | verified|
| 1 | 2000001 | 200.001,00 |not_verified|
+-------------+----------------+----------------+------------+
table costumer
+-------------+----------------+----------------+
| costumer_id | name | balance |
+-------------+----------------+----------------+
| 1 | bond | 3000001 |
+-------------+----------------+----------------+
The scenario is:
when there's incoming message, it will be saved in inbox.TextDecoded
The new incoming message will be checked whether it contains balance.refill_nominal or not
If no.2 is true then balance.status will be verified
If no.3 done then the value of costumer.balance will be added with the value of balance.refill_balance
I have tried this trigger for the balance.status update
DELIMITER $$
CREATE TRIGGER verify_refill AFTER INSERT ON inbox
FOR EACH ROW BEGIN
UPDATE balance
SET status='verified'
WHERE refill_nominal=(SELECT balance.refill_nominal FROM balance JOIN inbox ON NEW.inbox.TextDecoded LIKE CONCAT('%',balance.refill_nominal,'%') AND balance.status='not_verified';
END $$
And try this trigger for adding the costumer.balance
DELIMITER
CREATE TRIGGER add_balance AFTER UPDATE ON balance
FOR EACH ROW BEGIN
UPDATE costumer
SET balance=OLD.balance+NEW.refill_balance
WHERE
costumer_id=NEW.costumer_id
END $$
None of them work. Please help me..

mysql - The target table of the UPDATE is not updatable using trigger

update 2nd and 3rd row table after update 1st the row on same table
store procedure or trigger after update in table (using mysql)
id |Is_executed | Is_Ready
1 | 0 |
2 | | 0
3 | | 0
if i update is_executed to '1' of id is 1 then i want following row update
id |Is_executed | Is_Ready
1 | 1 |
2 | | 1
3 | | 1
using trigger or store procedure
Your desired business logic isn't entirely clear from your question, but it sounds like you want a trigger along the following lines:
DELIMITER ;;
CREATE TRIGGER my_trigger AFTER UPDATE ON my_table FOR EACH ROW
IF NEW.id = 1 AND OLD.Is_executed = 0 AND NEW.Is_executed = 1 THEN
UPDATE my_table SET Is_Ready = 1 WHERE id IN (2,3);
END IF
;;
DELIMITER ;