I have another problem with triggers using MySQL. These two tables namely debt and profile are now connected by a trigger that is solved from my other question here in stackoverflow.
CREATE TRIGGER `bal_load_debt` BEFORE INSERT ON `debt`
FOR EACH ROW BEGIN
UPDATE `profile`
SET `bal` = `bal` + NEW.`amount`
WHERE `profile`.`id` = NEW.`profile_id`;
END
DEBT
+--+----------+------+
|ID|PROFILE_ID|AMOUNT|
+--+----------+------+
|1 |1 |500 |
+--+----------+------+
PROFILE
+--+-------+-----+------+
|ID|BALANCE|LIMIT|STATUS|
+--+-------+-----+------+
|1 |500 |650 |OPEN |
|2 |300 |500 |OPEN |
+--+-------+-----+------+
My new problem is if the new balance is greater or equal than the limit, the status should be changed to CLOSE. I tried modifying the trigger bal_load_debt by adding
IF `balance` >= `limit`
THEN UPDATE `profile`
SET `status` = 'CLOSE';
END IF;
but results to an error #1054 Unknown column 'balance' in 'field list'
I also tried creating another trigger of an after update on profile table but I think the syntax used is not that effective :(
The goal is if I add another row at debt like:
+--+----------+------+
|ID|PROFILE_ID|AMOUNT|
+--+----------+------+
|1 |1 |500 |
|2 |1 |150 |
+--+----------+------+
The profile table should look like this:
+--+-------+-----+------+
|ID|BALANCE|LIMIT|STATUS|
+--+-------+-----+------+
|1 |650 |650 |CLOSE |
|2 |300 |500 |OPEN |
+--+-------+-----+------+
I'll be very glad of your helping hands :)