问题描述
我有一个带有smallInteger列的表,其中2条记录为零值。
如果我在下面运行查询,则返回结果:
select `status` from users where `status` = 'confirmed'
怎么可能?
实际上任何字符串都将返回此记录。
解决方法
mysql会将CHAR
的值强制转换为SMALLINT
,因为'confirmed'
不是数字,它将返回0
所以这:0 = 'confirmed'
被解释为这个0 = CAST('confirmed as SMALLINT)
',其值为0 = 0
因此,当列status
为0
时,它等于'confirmed'
作为SMALLINT