My Prestashop table ps_stock available has a column called quantity where the stock of the product is stored. This table is updated by an app, changing the values of the quantity column.
I need a trigger in that table. Every time the quantity of a product is changed I need to extract which products are effected on that table to insert them in another table
I have tried a trigger like this:
IF (OLD.quantity <> NEW.quantity)
THEN
INSERT IGNORE INTO ps_ebay_product_modified (id_ebay_product_modified,id_ebay_profile,id_product)
SELECT ps_ebay_product.id_ebay_product,id_ebay_profile,id_product
FROM ps_ebay_product INNER JOIN ps_stock_available
WHERE OLD.quantity <> NEW.quantity;
END IF
What I need is a selection of the products that have been changed.
This should work
CREATE TRIGGER mytrigger AFTER UPDATE ON ps_stock
FOR EACH ROW
BEGIN
-- do some stuff here
END;
Related
I have these two tables
**Table tb_data**
tb_id
timestamp
pagid
proid
status
(and many more)
**Table tb_units**
pag_id
pag_sn
user
latest_profile
latest_status
latest_feedback
latest_timestamp
Whenever a new row is created in tb_data, i would like some values updated in tb_units. In tb_units pag_id is unique and every number only exists once.
How do I trigger this, so the new values in tb_data are updated in tb_units? pagid equals pag_id and the corresponding values proid should update latest_profil, status should update latest_status, timestamp should update latest_timestamp
In the end I would like to end up with the latest pagid input to tb_data to be available in tb_units, since tb_data will contain multiple rows from the same pagid
I hvae tried several different aproaches, and have read a lot of examples, but I just don't get how these triggers work!
Latest example, that doesn't work:
CREATE TRIGGER update_units
AFTER INSERT ON tb_data
BEGIN
UPDATE tb_units
SET latest_profile = tb_data.9C,
latest_status = tb_data.91
WHERE pag_id = tb_data.86;
END
Not sure but something like this should work
CREATE TRIGGER update_units
AFTER INSERT ON tb_data
BEGIN
UPDATE tb_units
SET latest_profile = new.proid,
latest_status = new.status
WHERE pag_id = new.pagid;
END
I have a table called userlikes that has columns give_like_id, receive_like_id.
I also have a users table with count_likes and id columns. Every time a new record is inserted into userlikes table, I want to decrease by -1 in countlikes table.
userlikes: give_like_id, receive_like_id
users: id, count_likes
I tried this code, but it's not working.
CREATE TRIGGER `update_count_likes`
AFTER INSERT
ON `user_likes`
FOR EACH ROW
UPDATE users
SET users.likes_count = users.likes_count - 1
WHERE users.id = user_likes.give_like_id
hey, the answer for it:
CREATE TRIGGER `update_count_likes` AFTER INSERT ON `user_likes`
FOR EACH ROW UPDATE users
SET users.likes_count
= users.likes_count -1
WHERE users.id=NEW.give_like_id
I have two tables:
Table 1 : parts
id (primary key)
code
title
quantity
Table 2 : bill_items
id (primary key)
bill_id
parts_id: refers to primary key of table parts
qty
I would like to update parts table - qty every time I create a row in table bill_items. The qty in parts table is to be decremented by qty of bill_items. There may be N number of updates to table bill_items in one go. Would like to use a single INSERT....ON DUPLICATE or UPDATE statement.
Thank you for your time.
I think for this case better using trigger :
DELIMITER $$
CREATE
TRIGGER `bill_items_after_insert` AFTER INSERT
ON `bill_items`
FOR EACH ROW BEGIN
UPDATE parts set quantity = quantity - NEW.qty WHERE id = NEW.parts_id;
END$$
DELIMITER ;
I suggest you also make trigger for UPDATE and DELETE also for data consistency.
UPDATED
Based on your comment, it is possible to use normal insert and update using transaction for consistency data :
START TRANSACTION;
INSERT INTO bill_items (bill_id, parts_id, qty) VALUES (your_bill_id,your_parts_id,your_qty);
UPDATE parts SET quantity = quantity - your_qty WHERE id = your_parts_id;
COMMIT;
I have these two table called "cases" and attendance respectively which has four columns:
cases-
id empid reaction date_t
1 EMP12654 interested 2017-09-22
attendance-
id empid logintime logouttime date_t flag workinghours call_att
1 EMP12654 00:14:49 05:14:49 2017-09-18 set 6 1
What I want to do is create a trigger on cases table that updates call_att column of attendance table with number of entries in reaction column of cases table, this is what I have tried so far
CREATE DEFINER=`root`#`localhost` TRIGGER `number_call`
AFTER INSERT ON `cases` FOR EACH ROW
BEGIN UPDATE attendance set call_att=call_att +1
WHERE empid=new.empid AND date_t=new.date_t; END
But that doesn't seem to work. I am quite new to triggers.
try this
CREATE TRIGGER number_call
AFTER INSERT ON cases
FOR EACH ROW
BEGIN
UPDATE attendance set call_att=(select count(*) from cases where empid=NEW.empid )
date_t=NEW.date_t;
END
I have two tables named CustomerGoods(with column quantity) and Stock(with column Availablequantity). I want a trigger to occur when I insert into the CustomerGoods so that the quantity is added into Availablequantity in the Stock table.
You have not provided proper and to the point problem, but as per my understanding from what you have written.
Please try this.
CREATE
TRIGGER `AvailablequantityTrig` AFTER INSERT ON `CustomerGoods`
FOR EACH ROW BEGIN
UPDATE Availablequantity set quantity=quantity+1 where GoodsID = NEW.id;
END;
Here NEW.id is the id of goods and GoodsID is the id of goods in Availability table..
Please change your column names as required..
Thanks.