I want it to do is upon any insert of new rows or edit of existing rows on B, specific the 'last_seen' column would be updated on A.
This is what I have so far:
CREATE TRIGGER insert_records
AFTER UPDATE ON loan_records
FOR EACH ROW
Update temp_card A
INNER JOIN loan_records B
SET A.last_seen = CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no
and this
CREATE TRIGGER insert_records2
AFTER INSERT ON loan_records
FOR EACH ROW
Update temp_card A
INNER JOIN loan_records B
SET A.last_seen =CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no
currently this updates all 'last_seen' columns in A, regardless whether or not they've been updated.
You don't need to JOIN both tables. Just use NEW.card_no in WHERE clause of your UPDATE statement.
Here is how both (AFTER INSERT and AFTER UPDATE) your triggers might look like
CREATE TRIGGER tg_loan_records_insert
AFTER INSERT ON loan_records
FOR EACH ROW
UPDATE temp_card
SET last_seen = NOW()
WHERE card_no = NEW.card_no;
CREATE TRIGGER tg_loan_records_update
AFTER UPDATE ON loan_records
FOR EACH ROW
UPDATE temp_card
SET last_seen = NOW()
WHERE card_no = NEW.card_no;
Here is SQLFiddle demo
you should refer to only currently updated id.
CREATE TRIGGER insert_records2
AFTER INSERT ON loan_records
FOR EACH ROW
Update temp_card A
INNER JOIN loan_records B
SET A.last_seen =CURRENT_TIMESTAMP
WHERE A.card_no = B.card_no and a.id = OLD.id
For more details on how to use for before update, after update please refer
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
Related
I want to use a trigger to automatically update another table but I'm having some problems with it.
DELIMITER $$
DROP TRIGGER IF EXISTS `trigger1` $$
CREATE TRIGGER `trigger1`
AFTER UPDATE ON `table1` FOR EACH ROW
UPDATE `table4`
inner join (SELECT o.`Name`,
o.Date,
(o.`Availability` * (c.Rate)) total
FROM `table2` o
LEFT JOIN `table1` r
ON o.`Name` = r.`Name`
AND o.Date = r.Date
LEFT JOIN (SELECT table3.`Name`,Choke, Rate FROM table3
left join `table1` as w
on table3.`Name` = w.`Name`
and table3.Choke = w.`Size`
where w.`Name` = table3.`Name`
and table3.Date <= w.Date
ORDER BY table3.Date DESC
LIMIT 1) c
ON c.`Name` = o.`Name`)x
set `Contribution` = x.total
where (`table4`.Date) = x.Date and `table4`.`Name` = x.`Name`;
END $$
DELIMITER ;
I would like to use the date from table1 row (that is the table which triggers the trigger) in my left join named c. As it stands c.Rate gives the same value every time because it uses the default table1.
If the row being updated has a date of '2022-01-13' then I want the date used at the line asterisked
and table3.Date <= w.Date
I want w.Date to be '2022-01-13'. But as it stands I can't get that and all the c.Rate give the same value.
Thanks.
The lack of consistent indentation and capitalisation makes your query almost impossible to read. Instead of obfuscating what is going on by using table1, table2, table3 & table4 you would be better off using the real table names, as it will make more sense to anyone trying to read it.
Your current update query makes little sense with the repeated left joins back to the originating table1 but it is hard to be sure given the lack of supporting information in your question. Your first left join to table1, aliased as r, does not get used anywhere. Your second left join to table1, aliased as w, is then referenced in the where clause which turns it into an inner join.
I suggest you update your question with the CREATE TABLE statements and some sample data to show the values before and after executing your update and the trigger update. Your current update query is definitely not the most efficient way of achieving your goal.
I don't really understand your question but it seems that you all are asking is how to use the value from the table1 row being updated? In which case the answer is simply -
and w.Date = NEW.Date
where NEW references the post-update version of the table1 row.
From 25.3.1 Trigger Syntax and Examples -
Within the trigger body, the OLD and NEW keywords enable you to access
columns in the rows affected by a trigger. OLD and NEW are MySQL
extensions to triggers; they are not case-sensitive.
In an INSERT trigger, only NEW.col_name can be used; there is no old
row. In a DELETE trigger, only OLD.col_name can be used; there is no
new row. In an UPDATE trigger, you can use OLD.col_name to refer to
the columns of a row before it is updated and NEW.col_name to refer to
the columns of the row after it is updated.
Please I need your help. I have been working on a trigger statement and it seem not to be working.
I need help on writing a trigger to update a table called membership when a column on another table called serialcode is updated.
DETAILS: I have two tables called membership and serialcode. The membership table has the following columns
id fullname email date state sn
and the serialcode table has only
id sn
I want the sn in membership to be updated with the value of sn in serialcode is updated where the id in membership table is same as the id in serialcode table.
Here is what I have been able to write below
CREATE TRIGGER MembershipSerialCode
BEFORE UPDATE ON serialcode
FOR EACH ROW
BEGIN
if :new.sn != :old.sn
then
UPDATE membership m
set membership.sn = :new.sn
where m.id = :new.id;
end if;
END;
Thank you.
Mike
Can you try this one and let me know if you have luck on implementing it?
CREATE Trigger TriggerName
ON SerialNumberTable FOR UPDATE
AS
IF (Update(sn)) -- This is the column on SerialCodeTable that will be updated first
BEGIN
IF EXISTS (Select i.id
FROM Deleted d INNER JOIN Inserted i
ON d.id = i.id
AND d.sn <> i.sn)
BEGIN
UPDATE MembershipTable SET MembershipTable.sn = i.sn
FROM Inserted i
WHERE MembershipTable.id = i.id
END
END
I want to do simple trigger on delete in table X which will change value in table Table which has the same id.
UPDATE Table T
SET T.spots = T.spots +1
Where T.id = delete.id
But it doesnt work. I am not sure if "delete.columnName" works.
As mentioned by fejese in the comments, use
UPDATE Table T
SET T.spots = T.spots +1
Where T.id = OLD.id -- Use OLD.id instead of delete.id
I created a trigger like:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = '0';
END IF ;
It works as expected, when I update any row in the table_1 the one with key=0 in the other table is updated.
Since there is a field key in both tables, I want to update in the second table to row who has the same key of the updated row in table_1. I can't figure out how to use table_2.key inside the IF block.
You can use the new or old keyword (in case you change the key value, you may want to use one or the other). It let's you access all the values from the record that was updated on table_1:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = new.key;
END IF ;
You can see an example on this fiddle
try this:
CREATE TRIGGER `update_tb2` AFTER UPDATE ON `table_1` FOR EACH ROW
IF new.parameter = 'param1'
THEN UPDATE table_2 SET table_2.input = '10' WHERE table_2.key = old.key;
END IF ;
I'm developing a database that needs to update a field in one table when another table is updated. However, the table that is being updated doesn't contain the values I need... it just contains an ID to a third table that has that value. Basically, I'm looking for something that does this:
CREATE TRIGGER au_TableA AFTER UPDATE TableA
FOR EACH ROW BEGIN
UPDATE TableB SET points=TableC.points
WHERE TableC.cID=NEW.cID;
END
cID, of course, being present in both TableA and TableC.
Try this query -
UPDATE
TableB b
JOIN TableC с
ON c.cID = b.cID
SET
b.points = c.points
WHERE c.cID=NEW.cID;