I am creating a trigger to Update a column TEST if column receipt_on = Quarterly Interest
Then If so it should find the sum in column receipt_amount and join another table loan
on column l_app_file_id from table Loan on column r_app_file_id from table receipt_history
also the month name from column receipt_date from table receipt_history should be the same as the current month, however I am not sure as to how to entirely structure this trigger
-- Trigger DDL Statements
DELIMITER $$
USE `lms`$$
CREATE
DEFINER=`root`@`localhost`
TRIGGER `lms`.`updateloan`
BEFORE UPDATE ON `lms`.`receipt_history`
FOR EACH ROW
BEGIN
if new.receipt_on='Quarterly Interest' then
SET new.TEST=SUM(receipt_amount)
join loan l on
l.l_app_file_id=r.r_app_file_id
WHERE r_app_file_id=l_app_file_id
and monthname(receipt_date)=MONTHNAME(now())
end if;
END$$
As you now know you cannot join in a
setstatement.You need to use a select statement for that.
This construct will work however
set i:= (select sum(x) from a);I personally prefer the
SELECT something INTO avariablesyntax, but that’s just a matter of taste.Note that:
the join criterion is already a (kind of)
whereclause, so you don’t have to repeat that in thewhereclause.Each and every statement in the trigger needs to be terminated by a
;.