1

i want a trigger to be able to change the value of status=1 when balance is 0 and status=0 when the value of balance is not equal to 0 after update only ..

Mysql Table

3
  • Here you find something on How to Ask and how to build a minimal reproducible example Commented Aug 19, 2016 at 11:42
  • When do you want to fire a trigger ? After insert ?? Commented Aug 19, 2016 at 11:48
  • @priyanshu after update Commented Aug 19, 2016 at 11:55

2 Answers 2

3

Use new row to both access and change values before the update is actioned.

create trigger mytrigger
before update on mytable
for each row
set new.status = (new.balance = 0);

Conveniently (in MySQL), true is 1 and false is 0.

Sign up to request clarification or add additional context in comments.

Comments

2
CREATE  TRIGGER `db`.`trigger_name`
 AFTER INSERT ON `table_name` FOR EACH ROW
 BEGIN
 update table 
 set status = case when new.balance = 0 then '1' else 0 end;

It will check value of new balance value during INSERT and update the status. I hope this helps.

CREATE  TRIGGER `db`.`trigger_name`
 AFTER UPDATE ON `table_name` FOR EACH ROW
 BEGIN
 update table 
 set status = case when balance = 0 then '1' else 0 end;

1 Comment

see my answer, It will check whole table and update.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.