MySQL隐式转化整理

前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下。希望对大家有所帮助。

当我们对不同类型的值进行比较的时候,为了使得这些数值「可比较」(也可以称为类型的兼容性),MySQL会做一些隐式转化(Implicit type conversion)。比如下面的例子:

  1. mysql> SELECT 1+'1';
  2. -> 2
  3. mysql> SELECT CONCAT(2,' test');
  4. -> '2 test'

很明显,上面的SQL语句的执行过程中就出现了隐式转化。并且从结果们可以判断出,第一条SQL中,将字符串的“1”转换为数字1,而在第二条的SQL中,将数字2转换为字符串“2”。

MySQL也提供了CAST()函数。我们可以使用它明确的把数值转换为字符串。当使用CONCA()函数的时候,也可能会出现隐式转化,因为它希望的参数为字符串形式,但是如果我们传递的不是字符串呢:

  1. mysql> SELECT 38.8, CAST(38.8 AS CHAR);
  2. -> 38.8, '38.8'
  3. mysql> SELECT 38.8, CONCAT(38.8);
  4. -> 38.8, '38.8'

隐式转化规则

官方文档中关于隐式转化的规则是如下描述的:

If one or both arguments are NULL, the result of the comparison is NULL, except for the NULL-safe <=> equality comparison operator. For NULL <=> NULL, the result is true. No conversion is needed.

  • If both arguments in a comparison operation are strings, they are compared as strings.

  • If both arguments are integers, they are compared as integers.

  • Hexadecimal values are treated as binary strings if not compared to a number.

  • If one of the arguments is a TIMESTAMP or DATETIME column and the other argument is a constant, the constant is converted to a timestamp before the comparison is performed. This is done to be more ODBC-friendly. Note that this is not done for the arguments to IN()! To be safe, always use complete datetime, date, or time strings when doing comparisons. For example, to achieve best results when using BETWEEN with date or time values, use CAST() to explicitly convert the values to the desired data type.

    A single-row subquery from a table or tables is not considered a constant. For example, if a subquery returns an integer to be compared to a DATETIME value, the comparison is done as two integers. The integer is not converted to a temporal value. To compare the operands as DATETIME values, use CAST() to explicitly convert the subquery value to DATETIME.

  • If one of the arguments is a decimal value, comparison depends on the other argument. The arguments are compared as decimal values if the other argument is a decimal or integer value, or as floating-point values if the other argument is a floating-point value.

  • In all other cases, the arguments are compared as floating-point (real) numbers.

翻译为中文就是:

  • 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换
  • 两个参数都是字符串,会按照字符串来比较,不做类型转换
  • 两个参数都是整数,按照整数来比较,不做类型转换
  • 十六进制的值和非数字做比较时,会被当做二进制串
  • 有一个参数是 TIMESTAMP 或 DATETIME,并且另外一个参数是常量,常量会被转换为 timestamp
  • 有一个参数是 decimal 类型,如果另外一个参数是 decimal 或者整数,会将整数转换为 decimal 后进行比较,如果另外一个参数是浮点数,则会把 decimal 转换为浮点数进行比较
  • 所有其他情况下,两个参数都会被转换为浮点数再进行比较

注意点

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

  1. mysql> select * from test;
  2. +----+-------+-----------+
  3. | id | name | password |
  4. +----+-------+-----------+
  5. | 1 | test1 | password1 |
  6. | 2 | test2 | password2 |
  7. +----+-------+-----------+
  8. 2 rows in set (0.00 sec)
  9. mysql> select * from test where name = 'test1' and password = 0;
  10. +----+-------+-----------+
  11. | id | name | password |
  12. +----+-------+-----------+
  13. | 1 | test1 | password1 |
  14. +----+-------+-----------+
  15. 1 row in set, 1 warning (0.00 sec)
  16. mysql> show warnings;
  17. +---------+------+-----------------------------------------------+
  18. | Level | Code | Message |
  19. +---------+------+-----------------------------------------------+
  20. | Warning | 1292 | Truncated incorrect DOUBLE value: 'password1' |
  21. +---------+------+-----------------------------------------------+
  22. 1 row in set (0.00 sec)

相信上面的例子,一些机灵的同学可以发现其实上面的例子也可以做sql注入。

假设网站的登录那块做的比较挫,使用下面的方式:

  1. SELECT * FROM users WHERE username = '$_POST["username"]' AND password = '$_POST["password"]'

如果username输入的是a' OR 1='1,那么password随便输入,这样就生成了下面的查询:

  1. SELECT * FROM users WHERE username = 'a' OR 1='1' AND password = 'anyvalue'

就有可能登录系统。其实如果攻击者看过了这篇文章,那么就可以利用隐式转化来进行登录了。如下:

  1. mysql> select * from test;
  2. +----+-------+-----------+
  3. | id | name | password |
  4. +----+-------+-----------+
  5. | 1 | test1 | password1 |
  6. | 2 | test2 | password2 |
  7. | 3 | aaa | aaaa |
  8. | 4 | 55aaa | 55aaaa |
  9. +----+-------+-----------+
  10. 4 rows in set (0.00 sec)
  11. mysql> select * from test where name = 'a' + '55';
  12. +----+-------+----------+
  13. | id | name | password |
  14. +----+-------+----------+
  15. | 4 | 55aaa | 55aaaa |
  16. +----+-------+----------+
  17. 1 row in set, 5 warnings (0.00 sec)

之所以出现上述的原因是因为:

  1. mysql> select '55aaa' = 55;
  2. +--------------+
  3. | '55aaa' = 55 |
  4. +--------------+
  5. | 1 |
  6. +--------------+
  7. 1 row in set, 1 warning (0.00 sec)
  8. mysql> select 'a' + '55';
  9. +------------+
  10. | 'a' + '55' |
  11. +------------+
  12. | 55 |
  13. +------------+
  14. 1 row in set, 1 warning (0.00 sec)

下面通过一些例子来复习一下上面的转换规则:

  1. mysql> select 1+1;
  2. +-----+
  3. | 1+1 |
  4. +-----+
  5. | 2 |
  6. +-----+
  7. 1 row in set (0.00 sec)
  8. mysql> select 'aa' + 1;
  9. +----------+
  10. | 'aa' + 1 |
  11. +----------+
  12. | 1 |
  13. +----------+
  14. 1 row in set, 1 warning (0.00 sec)
  15. mysql> show warnings;
  16. +---------+------+----------------------------------------+
  17. | Level | Code | Message |
  18. +---------+------+----------------------------------------+
  19. | Warning | 1292 | Truncated incorrect DOUBLE value: 'aa' |
  20. +---------+------+----------------------------------------+
  21. 1 row in set (0.00 sec)

把字符串“aa”和1进行求和,得到1,因为“aa”和数字1的类型不同,MySQL官方文档告诉我们:

When an operator is used with operands of different types, type conversion occurs to make the operands compatible.

查看warnings可以看到隐式转化把字符串转为了double类型。但是因为字符串是非数字型的,所以就会被转换为0,因此最终计算的是0+1=1

上面的例子是类型不同,所以出现了隐式转化,那么如果我们使用相同类型的值进行运算呢?

  1. mysql> select 'a' + 'b';
  2. +-----------+
  3. | 'a' + 'b' |
  4. +-----------+
  5. | 0 |
  6. +-----------+
  7. 1 row in set, 2 warnings (0.00 sec)
  8. mysql> show warnings;
  9. +---------+------+---------------------------------------+
  10. | Level | Code | Message |
  11. +---------+------+---------------------------------------+
  12. | Warning | 1292 | Truncated incorrect DOUBLE value: 'a' |
  13. | Warning | 1292 | Truncated incorrect DOUBLE value: 'b' |
  14. +---------+------+---------------------------------------+
  15. 2 rows in set (0.00 sec)

是不是有点郁闷呢?

之所以出现这种情况,是因为+为算术操作符arithmetic operator 这样就可以解释为什么ab都转换为double了。因为转换之后其实就是:0+0=0了。

在看一个例子:

  1. mysql> select 'a'+'b'='c';
  2. +-------------+
  3. | 'a'+'b'='c' |
  4. +-------------+
  5. | 1 |
  6. +-------------+
  7. 1 row in set, 3 warnings (0.00 sec)
  8. mysql> show warnings;
  9. +---------+------+---------------------------------------+
  10. | Level | Code | Message |
  11. +---------+------+---------------------------------------+
  12. | Warning | 1292 | Truncated incorrect DOUBLE value: 'a' |
  13. | Warning | 1292 | Truncated incorrect DOUBLE value: 'b' |
  14. | Warning | 1292 | Truncated incorrect DOUBLE value: 'c' |
  15. +---------+------+---------------------------------------+
  16. 3 rows in set (0.00 sec)

现在就看也很好的理解上面的例子了吧。a+b=c结果为1,1在MySQL中可以理解为TRUE,因为'a'+'b'的结果为0,c也会隐式转化为0,因此比较其实是:0=0也就是true,也就是1.

第二个需要注意点就是防止多查询或者删除数据

  1. mysql> select * from test;
  2. +----+-------+-----------+
  3. | id | name | password |
  4. +----+-------+-----------+
  5. | 1 | test1 | password1 |
  6. | 2 | test2 | password2 |
  7. | 3 | aaa | aaaa |
  8. | 4 | 55aaa | 55aaaa |
  9. | 5 | 1212 | aaa |
  10. | 6 | 1212a | aaa |
  11. +----+-------+-----------+
  12. 6 rows in set (0.00 sec)
  13. mysql> select * from test where name = 1212;
  14. +----+-------+----------+
  15. | id | name | password |
  16. +----+-------+----------+
  17. | 5 | 1212 | aaa |
  18. | 6 | 1212a | aaa |
  19. +----+-------+----------+
  20. 2 rows in set, 5 warnings (0.00 sec)
  21. mysql> select * from test where name = '1212';
  22. +----+------+----------+
  23. | id | name | password |
  24. +----+------+----------+
  25. | 5 | 1212 | aaa |
  26. +----+------+----------+
  27. 1 row in set (0.00 sec)

​ 上面的例子本意是查询id为5的那一条记录,结果把id为6的那一条也查询出来了。我想说明什么情况呢?有时候我们的数据库表中的一些列是varchar类型,但是存储的值为‘1123’这种的纯数字的字符串值,一些同学写sql的时候又不习惯加引号。这样当进行select,update或者delete的时候就可能会多操作一些数据。所以应该加引号的地方别忘记了。

关于字符串转数字的一些说明


  1. mysql> select 'a' = 0;
  2. +---------+
  3. | 'a' = 0 |
  4. +---------+
  5. | 1 |
  6. +---------+
  7. 1 row in set, 1 warning (0.00 sec)
  8. mysql> select '1a' = 1;
  9. +----------+
  10. | '1a' = 1 |
  11. +----------+
  12. | 1 |
  13. +----------+
  14. 1 row in set, 1 warning (0.00 sec)
  15. mysql> select '1a1b' = 1;
  16. +------------+
  17. | '1a1b' = 1 |
  18. +------------+
  19. | 1 |
  20. +------------+
  21. 1 row in set, 1 warning (0.00 sec)
  22. mysql> select '1a2b3' = 1;
  23. +-------------+
  24. | '1a2b3' = 1 |
  25. +-------------+
  26. | 1 |
  27. +-------------+
  28. 1 row in set, 1 warning (0.00 sec)
  29. mysql> select 'a1b2c3' = 0;
  30. +--------------+
  31. | 'a1b2c3' = 0 |
  32. +--------------+
  33. | 1 |
  34. +--------------+
  35. 1 row in set, 1 warning (0.00 sec)

从上面的例子可以看出,当把字符串转为数字的时候,其实是从左边开始处理的。

  • 如果字符串的第一个字符就是非数字的字符,那么转换为数字就是0
  • 如果字符串以数字开头
  • 如果字符串中都是数字,那么转换为数字就是整个字符串对应的数字
  • 如果字符串中存在非数字,那么转换为的数字就是开头的那些数字对应的值

如果你有其他更好的例子,或者被隐式转化坑过的情况,欢迎分享。

引用自:http://www.cnblogs.com/rollenholt/p/5442825.html

参考资料

mysql的隐式转化的更多相关文章

  1. MySQL隐式转化整理

    MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...

  2. MySQL的隐式类型转换整理总结

    当我们对不同类型的值进行比较的时候,为了使得这些数值「可比较」(也可以称为类型的兼容性),MySQL会做一些隐式转化(Implicit type conversion). 比如下面的例子:   1 2 ...

  3. scala学习笔记5 (隐式转化/参数/类)

    隐式转化: 隐式参数: 隐式类:

  4. hadoop cdh5的pig隐式转化(int到betyarray)不行了

    cdh3上,pig支持int到chararray的隐式转化,但到cdh5不行. pig code is as follows: %default Cleaned_Log /user/usergroup ...

  5. mysql的几种隐式转化

    1. 表定义是字符型,传入的是Int 2. 字符集不一致.表定义的字段是gbk,传入的是utf8:这种在存储过程中出现得比较多. 数据库的字符集utf8 mysql> show create d ...

  6. MYSQL的隐式类型转换

    官方文档中是这么说的 当操作者使用不同类型的操作数,操作数类型兼容的出现使 转换.一些 发生隐式转换.例如,MySQL会自动 将数字转换为字符串的必要,反之亦然. 也可以将数字转换为字符串明确 使用( ...

  7. Mysql隐式类型转换原则

    MySQL 的隐式类型转换原则: - 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换 ...

  8. Qt C++中的关键字explicit——防止隐式转换(也就是Java里的装箱),必须写清楚

    最近在复习QT,准备做项目了,QT Creator 默认生成的代码 explicit Dialog(QWidget *parent = 0)中,有这么一个关键字explicit,用来修饰构造函数.以前 ...

  9. C#之隐式与显示类型转换

    今天在看一篇有关数据类型的文章的时候,无意间看到了两个关键词,"隐式转换"与"显示转换",然后突然想起了当初开始学编程的时候,也总是在代码编译的时候遇到这样的问 ...

随机推荐

  1. UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-13: ordinal not i│ n range(128)

    python保持网页文件遇到的错误,归根结底还是编码问题,改一下要保存的数据为utf-8就好了. 如下最简单: import sys reload(sys) sys.setdefaultencodin ...

  2. Linux之:Ubuntu速学笔记(2)

    撰写日期:2016-7-3 18:20:39 基本内容包括:Flash player安装.编译安装PHP.写个简单的PHP程序:Java程序(Java需要使用“javac”命令编译一下才能执行) 一. ...

  3. HTML5学习总结-06 WebWorker

    一 WebWorkder 它允许开发人员编写能够长时间运行而不被用户所中断的后台程序,去执行事务或者逻辑,并同时保证页面对用户的响应.简而言之,就是允许JavaScript创建多个线程,但是子线程完全 ...

  4. How to set up an FTP server on Ubuntu 14.04

    How to set up an FTP server on Ubuntu 14.04 Setting up a fully-functional and highly secure FTP serv ...

  5. CSS-论css如何纯代码实现内凹圆角

    background-image: radial-gradient(200px at 50px 0px, #fff 50px, #4169E1 50px); 这是做内凹圆角的核心代码,就是背景图的ra ...

  6. 为什么springMVC和Mybatis逐渐流行起来了?

    http://www.zhihu.com/question/36032573 https://github.com/bingoohuang/eql

  7. linux安装ftp组件

    1   安装vsftpd组件 linux系统安装完后,有/etc/vsftpd/vsftpd.conf 文件,是vsftp的配置文件. [root@bogon ~]# yum -y install v ...

  8. HTML学习笔记——列表和table

    1>有序列表.无序列表和自定义列表 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &qu ...

  9. string.capwords()函数

    string.capwords()函数 string.capwords()函数,有需要的朋友可以参考下. 代码 : import syssys.path.append("C:/Python2 ...

  10. OpenCV图像金字塔:高斯金字塔、拉普拉斯金字塔与图片尺寸缩放

    这篇已经写得很好,真心给作者点个赞.题目都是直接转过来的,直接去看吧. Reference Link : http://blog.csdn.net/poem_qianmo/article/detail ...