奇怪的选择结果
我有一个表,其 ID 字段为 INT 类型。
我正在做一些数据验证并注意到这一点
SELECT * from mytable where id=94
并且
SELECT * from mytable where id='94'
工作得很好,但是如果我使用
SELECT * from mytable where id='94dssdfdfgsdfg2'
它会给我相同的结果!这怎么可能?
I have a table with an ID field of INT Type.
I am doing some data validation and noticed that
SELECT * from mytable where id=94
and
SELECT * from mytable where id='94'
works perfectly, but if I use
SELECT * from mytable where id='94dssdfdfgsdfg2'
it gave me the same result! How is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所目睹的称为“隐式数据转换”。
隐式与“显式”相反,意味着数据类型会在可能的情况下自动转换为要比较的列的数据类型。在本例中,
MYTABLE.id
是一个 INTeger 数据类型,因此如果用单引号括起来,MySQL 会将要比较的值转换为 INT(基于字符串的数据类型为 SQL)。由于转换,数据从字符串最左边的位置开始,在最后一个数字字符的末尾被截断。
What you've witnessed is called "implicit data conversion".
Implicit, the opposite of "explicit", means that the data type is automatically converted to the data type of the column being compared when possible. In this case,
MYTABLE.id
is an INTeger data type so MySQL will convert the value being compared to an INT if it is enclosed in single quotes (string based data type to SQL).Because of the conversion, the data is getting truncated at the end of the last numeric character after starting from the leftmost position in the string.
如果 MySql 的 string to int 函数的内部实现在解析字符串时删除第一个非数字之后的所有字符,这是可能的。
it would be possible if the internal implementation of MySql's string to int function dropped all characters from the string after the first non-numeric when parsing it.