I have some MySQL code:
CREATE TABLE test_table (
id int(11) NOT NULL AUTO_INCREMENT,
count int(10) unsigned DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
insert into test_table (count) values (1);
update test_table set count = IF( count -1 < 0, 0 , count -1 );
update test_table set count = IF( count -1 < 0, 0 , count -1 );
This should set count to zero if (count -1) < 0.
I get an error:
Error Code: 1264 Out of range value for column 'count' at row 1` instead.
This is due to count being an unsigned int. If I use a signed int it works.
Is this a bug in MySQL? I’m using: mysql Ver 14.14 Distrib 5.1.46sp1, for Win32 (ia32)
I think you should be doing this:
The value of
count - 1underflows whencountis zero.When I try your query, it doesn’t work… but it doesn’t fail in the same way as you see.