MySQL 的错误?选择 WHERE id=“1blah”

发布于 2024-10-06 23:38:35 字数 599 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她如夕阳 2024-10-13 23:38:35

它会自动将字符串“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.

谷夏 2024-10-13 23:38:35

发生这种情况是因为类型转换。由于您的列具有整数值,因此“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.

尴尬癌患者 2024-10-13 23:38:35

如果您没有将整数 id 括在引号中,那么它会起作用,并且您会如您所希望的那样收到错误。也就是说,

SELECT * FROM `product` WHERE `id` = 1;

1 row in set

可以工作,但会

SELECT * FROM `product` WHERE `id` = 1blah;

ERROR 1054 (42S22): Unknown column '1blah' in 'where clause'

出现错误。

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,

SELECT * FROM `product` WHERE `id` = 1;

1 row in set

works, while

SELECT * FROM `product` WHERE `id` = 1blah;

ERROR 1054 (42S22): Unknown column '1blah' in 'where clause'

errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文