MySQL 的错误?选择 WHERE id=“1blah”
MySQL 版本 5.0.67
看看这个非常简单的表,然后告诉我是否发现了 MySQL 错误,我尝试过搜索答案,但正如你可以想象的那样,找到正确的搜索词有点
CREATE TABLE `product` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `product` VALUES (1, 'jim');
INSERT INTO `product` VALUES (2, 'bob');
困难然后我可以选择以下内容
SELECT * FROM `product` WHERE `id` = '1';
显然这会返回一行,但是,这也是
SELECT * FROM `product` WHERE `id` = '1blah';
……为什么?这肯定是错的还是我疯了?在我用 MySQL 提交错误报告之前,我会多爬一点网络。
MySQL Version 5.0.67
Take a look at this very simple table and tell me if I have found a MySQL bug, I have tried to search for an answer but as you can imagine it's a bit hard to come up with the right search terms
CREATE TABLE `product` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(60) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
INSERT INTO `product` VALUES (1, 'jim');
INSERT INTO `product` VALUES (2, 'bob');
From there I can then select the following
SELECT * FROM `product` WHERE `id` = '1';
Obviously this returns a row, but then, so does this
SELECT * FROM `product` WHERE `id` = '1blah';
Erm... WHY? Surely this is wrong or am I going mad? Will crawl the web a bit more before I file a bug report with MySQL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它会自动将字符串“1blah”转换为整数。由于字符串以“1”开头,因此生成的整数只是 1。
因此,它只是试图做正确的事情,尽管它可能看起来有点违反直觉。
It's automatically converting the string "1blah" into an integer. As the string begins with a "1" the resultant integer is simply 1.
As such, it's just trying to do the right thing, even though it might seem a bit counter-intuitive.
发生这种情况是因为类型转换。由于您的列具有整数值,因此“1blah”将转换为 1。请参阅 http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html 了解更多详细信息。
This happens because of type conversion. Since your column has integer value, '1blah' is converted to 1. Please, see http://dev.mysql.com/doc/refman/5.0/en/type-conversion.html for more details.
如果您没有将整数 id 括在引号中,那么它会起作用,并且您会如您所希望的那样收到错误。也就是说,
可以工作,但会
出现错误。
If you didn't enclose the integer id in quotes, it'd work and you'd get an error as you'd hope. That is,
works, while
errors.