我对 MySQL 并不陌生,但是今天我的代码中碰巧发生了一个奇怪的情况,这让我感到惊讶。有人可以解释为什么这会给出相同的结果吗?
SELECT * FROM `products` WHERE id = 12
和
SELECT * FROM `products` WHERE id = '12ABC'
在这两种情况下,我都选择了相同的记录,得到了相同的结果。我希望第二个不会给我任何返回?!我的 ID 字段是 int(11) unsigned,并且 auto_increment 标志打开。
最佳答案
来自 MySQL 文档:
When an operator is used with operands of different types, type conversion occurs to make the operands compatible
所以基本上,'12ABC'
被转换为 12
。
关于MySQL在where子句中自动将字符串转换为整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804639/