MySQL隐式转换的坑

数据库73

MySQL以以下规则描述比较操作如何进行转换:

MySQL隐式转换的坑

两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用

在实践中,人们发现,当将字符串与数字进行比较时,优先将字符串转换为数字。当字符串无法转换为数字时,将转换前缀数字。如果没有前缀,则数字为0。

[En]

In practice, it is found that when comparing a string with a number, priority is given to converting the string into a number. When the string cannot be converted into a number, the prefix number is converted. If there is no prefix, the number is 0.

举例说明,数据如下表user

MySQL隐式转换的坑

注意一个安全问题:假如 password 类型为字符串,查询条件为 int 0 则会匹配上。

MySQL隐式转换的坑

MySQL隐式转换的坑

由于 MySQL 隐式类型转换规则比较复杂,依赖 MySQL 隐式转换很容易出现各种想像不到的问题,
而且 MySQL 隐式类型转换本身也是非常耗费 MySQL 服务器性能的,所以非常不推荐这样使用。

输入验证码查看隐藏内容

扫描二维码关注本站微信公众号 Johngo学长
或者在微信里搜索 Johngo学长
回复 svip 获取验证码
wechat Johngo学长