I have created an event which will update the balance in table ‘try_event’ after every 5 minutes according to the balance and time at which the row is inserted.
EVENT code:
delimiter $$
CREATE EVENT `event`
ON SCHEDULE every 1 second
DO
begin
-- update balance by 2%
UPDATE try_event
SET Balance = case
WHEN timestampdiff(minute,date_created,current_timestamp) >0 and timestampdiff(minute,date_created,current_timestamp) MOD 5 = 0 and (balance>2000 and balance<3000) then Balance * 1.02
end;
end $$
delimiter ;
TABLE :
create table try_event(balance numeric(10,2) not null,date_created timestamp);
INSERTED ROWS:
insert into try_event values(2500,default);
insert into try_event values(1000,default);
but still it is giving the balance=2500 after 5 minutes.
When I remove “(balance>2000 and balance<3000)” the whole balance column is updated and result is:
2550
1020
I just created a test table on an instance of MySQL 5.5.29 (actually Percona Server, which is MySQL with some patches and new feature). It worked fine, it updated the 2500 balance and did not update the 1000 balance.
Here’s a check that shows that the update happened (including automatically updating the TIMESTAMP column):
I made slight changes to the event definition:
When using CASE expressions, it’s worthwhile to represent an “ELSE” clause because otherwise if the WHEN condition is false, and you have no ELSE clause, the result of the CASE expression is just NULL.
Is it possible your balance column is declared NOT NULL, so the UPDATE is trying to set balance to NULL and it’s simply failing?
I also remove the parentheses around the balance range comparison, but that shouldn’t make any difference.