奇怪的转换行为在mysql中 [英] Strange casting behavior in mysql

查看:242
本文介绍了奇怪的转换行为在mysql中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是代码

Here is the code

mysql> SELECT id FROM tbl WHERE id = '1h';
+----+
| id |
+----+
|  1 |
+----+
1 row in set

一个ID为1(但不是'1h')的字段。

这里是从MySQL文档中提取: http://dev.mysql.com/doc/refman/5.1/en/type-conversion.html

There is indeed a field with id 1 (but not '1h').
Here is an extraction from MySQL docs: http://dev.mysql.com/doc/refman/5.1/en/type-conversion.html

mysql> SELECT 1 > '6x';
    -> 0
mysql> SELECT 7 > '6x';
    -> 1

所以这个bug是记录在案的。问题是这样的行为的原因是什么,如何纠正它,使这不是用char符号铸造字符串?我可以转换所有字段值如

So this bug is documented, so to say. The question is what's the reason for such behavior and how to correct it to make this not cast strings with char symbols? I can cast all field values like

mysql> SELECT id FROM tbl WHERE cast(`id`, BINARY) = '1h';

但我不太喜欢这个变体

推荐答案

这不是一个错误。

解决方案是

从不使用隐式类型转换。

这篇关于奇怪的转换行为在mysql中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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