Mysql trigger after update New is wrong - mysql

I created a trigger that on a table update inserts row into another table, but one of the values that I pass is getting the old value of the table I am updating.
This is the trigger
DELIMITER $$
CREATE TRIGGER ordersHistory
AFTER UPDATE
ON orders FOR EACH ROW
BEGIN
IF (NEW.status <> OLD.status) THEN
INSERT INTO test (`test`) VALUES (NEW.ourNotes);
INSERT INTO test (`test`) VALUES (OLD.ourNotes);
END IF;
END $$
This insert at the table exactly the same thing.(the old value of ourNotes)

Related

How to write a MySql insert trigger based on inserted data value?

I want to wrote a trigger for table users. when new user added, if his Title is IT related, then create a record in IT contact list table.
So I wrote below trigger.
CREATE TRIGGER `test1` INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (username,Title);
END IF;
END;
It has error 'unknow column Title in the field list' But it did exist in table Users and IT_contact_list.So what's the issue?Thx.
Fixed and tested in workbench. Try this:
delimiter //
drop trigger if exists test1 //
CREATE TRIGGER `test1` AFTER INSERT ON `Users` FOR EACH ROW BEGIN
IF INSTR(new.Title,'IT') > 0
THEN
INSERT INTO IT_contact_list (name,title) value (new.username,new.Title);
END IF;
END//
insert into Users values('john','HardwareIT');
insert into Users values('bill','Hardware engineer');
select * From IT_contact_list;
--result set:
john HardwareIT

mysql trigger to update the same table from another table

I'm trying to set up a trigger which will update same row by inserting some additional data from another table. Field receive.iccid is blank, and I want it to be updated on every insert. However this trigger doesn't work
delimiter //
CREATE TRIGGER ins_iccid
AFTER INSERT ON receive
FOR EACH ROW
BEGIN
UPDATE receive SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END//
delimiter ;
turns out that I need to use 'BEFORE INSERT' to achieve what i was trying to
CREATE TRIGGER `ins_iccid` BEFORE INSERT ON `receive`
FOR EACH ROW
BEGIN
SET NEW.iccid = (SELECT goip.iccid FROM goip WHERE NEW.goipname=goip.name);
END

Make a trigger that copies new data from table to another table after insert

I have this code for the trigger:
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
SELECT Fecha FROM hyk50_0001
END;
but doesn't work, it says a syntax error but I didn't see it
I'm using Navicat. And hyk50_0001_copy it's a identical copy of hyk50_0001.
The target is take the new row of hyk50_0001 and INSERT in hyk50_0001_copy
I want put all the database, but if it doesn't work with only a value I can't progress.
I don't see a syntax error, but I assume you intend:
DELIMITER $$
CREATE TRIGGER `insert_after` AFTER INSERT ON `hyk50_0001`
FOR EACH ROW
BEGIN
INSERT INTO hyk50_0001_copy(Fecha)
VALUES (new.Fecha)
END;$$
DELIMITER ;
That is, you probably want to insert only the row that was just created.

how to insert just the row I updated in one table into another table by using trigger in myspl?

I am learning to create a trigger that can insert the row I update in one table into another table.
the query below is my creating trigger:
DELIMITER $$
CREATE TRIGGER update_edit_deliverycompany BEFORE UPDATE ON `deliverycompany`
FOR EACH ROW
BEGIN
INSERT INTO `editor_log` (`edit_table`, `edit_field`, `editor`, `edit_time`)
SELECT 'deliverycompany',`deliverycompany`.`CompanyID`,USER(),CURDATE() FROM `deliverycompany` ;
END$$
DELIMITER ;
when the trigger is fired, All rows in 'deliverycompany' are insert into the 'editor_log' rather than just the row I updated.
How to just select the row I update
if using where how can I locate the row I updated (any value in the row)
Try something like this
DELIMITER $$
CREATE TRIGGER update_edit_deliverycompany BEFORE UPDATE ON `deliverycompany`
FOR EACH ROW
BEGIN
INSERT INTO `editor_log` (`edit_table`, `edit_field`, `editor`, `edit_time`) VALUES (OLD.deliverycompany, OLD.deliverycompany, USER(),CURDATE()) ;
END$$
DELIMITER ;

mysql triggers to multiply two column values by using triggers

i have 4 columns in my table, vrms,irms,total,id.
i have created a trigger where i want to insert the result of the trigger in another table.
my code:
CREATE TRIGGER total_sum BEFORE INSERT ON meter5
FOR EACH ROW BEGIN
INSERT INTO hourly
SELECT vrms,irms,id,
NEW.irms * NEW.vrms AS total;
END
error:
not allowed to return a resultset from a trigger in mysql.
You need the trigger to be after insert something as
delimiter //
create trigger total_sum after insert on meter5
for each row
begin
insert into hourly (id,vrms,irms,total) values (new.id,new.vrms,new.irms,new.vrms*new.irms);
end ; //
delimiter ;