MySQL自动将字符串转换为where子句中的整数? [英] MySQL automatic string to integer casting in where clause?

查看:79
本文介绍了MySQL自动将字符串转换为where子句中的整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对MySQL并不陌生,但是今天巧合的是,我的代码中发生了一种奇怪的情况,这使我感到惊讶.有人可以解释为什么这会给我相同的结果吗?

I am not new to MySQL but a strange situation happened in my code today coincidently which got me surprised. Can someone explain why this gives me identical results?

SELECT * FROM `products` WHERE id = 12

SELECT * FROM `products` WHERE id = '12ABC'

在两种情况下,我得到的结果都是相同的,并且选择了相同的记录.我希望第二个人不会给我任何回报?我的ID字段是int(11),未签名,并且auto_increment标志处于打开状态.

In both cases I get the same result with the same record being selected. I would expect that second one would return me nothing?! My ID field is int(11) unsigned with auto_increment flag turned on.

推荐答案

来自MySQL文档:

当运算符与不同类型的操作数一起使用时,会发生类型转换以使操作数兼容

When an operator is used with operands of different types, type conversion occurs to make the operands compatible

文档

因此,基本上,'12ABC'被强制转换为12.

So basically, '12ABC' is cast to 12.

这篇关于MySQL自动将字符串转换为where子句中的整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆