MySQL的隐式类型转换整理总结
当我们对不同类型的值进行比较的时候,为了使得这些数值「可比较」(也可以称为类型的兼容性),MySQL会做一些隐式转化(Implicit type conversion)。
比如下面的例子:
|
1
2
3
4
|
mysql> SELECT 1+'1'; -> 2mysql> SELECT CONCAT(2,' test'); -> '2 test' |
很明显,上面的SQL语句的执行过程中就出现了隐式转化。并且从结果们可以判断出,第一条SQL中,将字符串的“1”转换为数字1,而在第二条的SQL中,将数字2转换为字符串“2”。
MySQL也提供了CAST()函数。我们可以使用它明确的把数值转换为字符串。当使用CONCA()函数的时候,也可能会出现隐式转化,因为它希望的参数为字符串形式,但是如果我们传递的不是字符串呢:
|
1
2
3
4
|
mysql> SELECT 38.8, CAST(38.8 AS CHAR); -> 38.8, '38.8'mysql> SELECT 38.8, CONCAT(38.8); -> 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
mysql> select * from test;+----+-------+-----------+| id | name | password |+----+-------+-----------+| 1 | test1 | password1 || 2 | test2 | password2 |+----+-------+-----------+2 rows in set (0.00 sec)mysql> select * from test where name = 'test1' and password = 0;+----+-------+-----------+| id | name | password |+----+-------+-----------+| 1 | test1 | password1 |+----+-------+-----------+1 row in set, 1 warning (0.00 sec)mysql> show warnings;+---------+------+-----------------------------------------------+| Level | Code | Message |+---------+------+-----------------------------------------------+| Warning | 1292 | Truncated incorrect DOUBLE value: 'password1' |+---------+------+-----------------------------------------------+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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
mysql> select * from test;+----+-------+-----------+| id | name | password |+----+-------+-----------+| 1 | test1 | password1 || 2 | test2 | password2 || 3 | aaa | aaaa || 4 | 55aaa | 55aaaa |+----+-------+-----------+4 rows in set (0.00 sec)mysql> select * from test where name = 'a' + '55';+----+-------+----------+| id | name | password |+----+-------+----------+| 4 | 55aaa | 55aaaa |+----+-------+----------+1 row in set, 5 warnings (0.00 sec) |
之所以出现上述的原因是因为:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
mysql> select '55aaa' = 55;+--------------+| '55aaa' = 55 |+--------------+| 1 |+--------------+1 row in set, 1 warning (0.00 sec)mysql> select 'a' + '55';+------------+| 'a' + '55' |+------------+| 55 |+------------+1 row in set, 1 warning (0.00 sec) |
下面通过一些例子来复习一下上面的转换规则:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
mysql> select 1+1;+-----+| 1+1 |+-----+| 2 |+-----+1 row in set (0.00 sec)mysql> select 'aa' + 1;+----------+| 'aa' + 1 |+----------+| 1 |+----------+1 row in set, 1 warning (0.00 sec)mysql> show warnings;+---------+------+----------------------------------------+| Level | Code | Message |+---------+------+----------------------------------------+| Warning | 1292 | Truncated incorrect DOUBLE value: 'aa' |+---------+------+----------------------------------------+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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
mysql> select 'a' + 'b';+-----------+| 'a' + 'b' |+-----------+| 0 |+-----------+1 row in set, 2 warnings (0.00 sec)mysql> show warnings;+---------+------+---------------------------------------+| Level | Code | Message |+---------+------+---------------------------------------+| Warning | 1292 | Truncated incorrect DOUBLE value: 'a' || Warning | 1292 | Truncated incorrect DOUBLE value: 'b' |+---------+------+---------------------------------------+2 rows in set (0.00 sec) |
是不是有点郁闷呢?
之所以出现这种情况,是因为+为算术操作符arithmetic operator 这样就可以解释为什么a和b都转换为double了。因为转换之后其实就是:0+0=0了。
再看一个例子:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
mysql> select 'a'+'b'='c';+-------------+| 'a'+'b'='c' |+-------------+| 1 |+-------------+1 row in set, 3 warnings (0.00 sec)mysql> show warnings;+---------+------+---------------------------------------+| Level | Code | Message |+---------+------+---------------------------------------+| Warning | 1292 | Truncated incorrect DOUBLE value: 'a' || Warning | 1292 | Truncated incorrect DOUBLE value: 'b' || Warning | 1292 | Truncated incorrect DOUBLE value: 'c' |+---------+------+---------------------------------------+3 rows in set (0.00 sec) |
现在就看也很好的理解上面的例子了吧。a+b=c结果为1,1在MySQL中可以理解为TRUE,因为'a'+'b'的结果为0,c也会隐式转化为0,因此比较其实是:0=0也就是true,也就是1.
第二个需要注意点就是防止多查询或者删除数据
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
mysql> select * from test;+----+-------+-----------+| id | name | password |+----+-------+-----------+| 1 | test1 | password1 || 2 | test2 | password2 || 3 | aaa | aaaa || 4 | 55aaa | 55aaaa || 5 | 1212 | aaa || 6 | 1212a | aaa |+----+-------+-----------+6 rows in set (0.00 sec)mysql> select * from test where name = 1212;+----+-------+----------+| id | name | password |+----+-------+----------+| 5 | 1212 | aaa || 6 | 1212a | aaa |+----+-------+----------+2 rows in set, 5 warnings (0.00 sec)mysql> select * from test where name = '1212';+----+------+----------+| id | name | password |+----+------+----------+| 5 | 1212 | aaa |+----+------+----------+1 row in set (0.00 sec) |
上面的例子本意是查询id为5的那一条记录,结果把id为6的那一条也查询出来了。我想说明什么情况呢?有时候我们的数据库表中的一些列是varchar类型,但是存储的值为‘1123'这种的纯数字的字符串值,一些同学写sql的时候又不习惯加引号。这样当进行select,update或者delete的时候就可能会多操作一些数据。所以应该加引号的地方别忘记了。
关于字符串转数字的一些说明
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
mysql> select 'a' = 0;+---------+| 'a' = 0 |+---------+| 1 |+---------+1 row in set, 1 warning (0.00 sec)mysql> select '1a' = 1;+----------+| '1a' = 1 |+----------+| 1 |+----------+1 row in set, 1 warning (0.00 sec)mysql> select '1a1b' = 1;+------------+| '1a1b' = 1 |+------------+| 1 |+------------+1 row in set, 1 warning (0.00 sec)mysql> select '1a2b3' = 1;+-------------+| '1a2b3' = 1 |+-------------+| 1 |+-------------+1 row in set, 1 warning (0.00 sec)mysql> select 'a1b2c3' = 0;+--------------+| 'a1b2c3' = 0 |+--------------+| 1 |+--------------+1 row in set, 1 warning (0.00 sec) |
从上面的例子可以看出,当把字符串转为数字的时候,其实是从左边开始处理的。
- 如果字符串的第一个字符就是非数字的字符,那么转换为数字就是0
- 如果字符串以数字开头
- 如果字符串中都是数字,那么转换为数字就是整个字符串对应的数字
- 如果字符串中存在非数字,那么转换为的数字就是开头的那些数字对应的值
总结
以上就是这篇文章的全部内容了,如果你有其他更好的例子,或者被隐式转化坑过的情况,欢迎分享。希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。
MySQL的隐式类型转换整理总结的更多相关文章
- MYSQL的隐式类型转换
官方文档中是这么说的 当操作者使用不同类型的操作数,操作数类型兼容的出现使 转换.一些 发生隐式转换.例如,MySQL会自动 将数字转换为字符串的必要,反之亦然. 也可以将数字转换为字符串明确 使用( ...
- MySQL隐式转化整理
MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
- Mysql隐式类型转换原则
MySQL 的隐式类型转换原则: - 两个参数至少有一个是 NULL 时,比较的结果也是 NULL,例外是使用 <=> 对两个 NULL 做比较时会返回 1,这两种情况都不需要做类型转换 ...
- mysql的隐式转化
MySQL隐式转化整理 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下.希望对大家有所帮 ...
- MYSQL隐式类型转换
MYSQL隐式类型转换 关于官方文档中的理解大致是: 如果两个参数比较,有至少一个NULL,结果就是NULL,除了是用NULL<=>NULL 会返回1.不做类型转换 两个参数都是字符串,按 ...
- js条件判断时隐式类型转换
Javascript 中,数字 0 为假,非0 均为真 在条件判断运算 == 中的转换规则是这样的: 如果比较的两者中有布尔值(Boolean),会把 Boolean 先转换为对应的 Number,即 ...
- dynamic_cast 和 static_cast 隐式类型转换的区别
首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion ...
- c++ operator操作符的两种用法:重载和隐式类型转换,string转其他基本数据类型的简洁实现string_cast
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换.对于操作符的重载,许多人都不陌生,但是估计不少人都不太熟悉operator的第二种用法,即自定义对象类型的隐式 ...
- C++的隐式类型转换与转换操作符
C++标准允许隐式类型转换,即对特定的类,在特定条件下,某些参数或变量将隐形转换成类对象(创建临时对象).如果这种转换代价很大(调用类的构造函数),隐式转换将影响性能.隐式转换的发生条件:函数调用中, ...
随机推荐
- java虚拟机之GC(转)
垃圾回收主要内容: 1. 那些内存需要回收? 2. 什么时候回收? 3. 如何回收? 垃圾回收主要针对运行时数据区那些区域? 运行时数据区的线程私有区域有:虚拟机栈,本地方法栈,程序计数器等: 栈中的 ...
- Python数据报协议以及sockersever模块的使用
一.基于UDP协议的套接字 UDP协议 UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式 ...
- 工作中遇到的vscode配合eslint完成保存为eslint格式
vscode个人设置 // vscode的个人设置配置 { "workbench.iconTheme": "vscode-icons", "workb ...
- ArcGIS 10.2之地图服务的发布、使用
2.发布地图服务 2.1 地图服务器的建立 打开ArcCatalog,在左侧的GIS Servers下,双击Add ArcGIS Server, 弹出添加界面,选择,Administer GIS服务项 ...
- 【起航计划 020】2015 起航计划 Android APIDemo的魔鬼步伐 19 App->Dialog Dialog样式
这个例子的主Activity定义在AlertDialogSamples.java 主要用来介绍类AlertDialog的用法,AlertDialog提供的功能是多样的: 显示消息给用户,并可提供一到三 ...
- 《ArcGIS Runtime SDK for Android开发笔记》——问题集:使用TextSymbol做标注显示乱码
1.前言 在14年的时候写过一篇博客关于ArcGIS for Android 10.1.1API 中文标注导致程序异常崩溃问题,但是当时并没有很好的解决这样一个问题,也并没有深入研究分析这样的一个异常 ...
- python_opencv应用系列1:图片读写
opencv的读写非常简单,主要用到的就是imread和imwrite两个函数 读取图片示例 import cv2 #imread(filename[, flags]) -> retval im ...
- day002-List类、泛型
1. 集合 集合是容器,可以存储任意类型的数据,集合的长度可变. 1.1 集合和数组的比较 1.2 集合分类 单列集合:每次存储时,存储一个元素(Collection),包括:list.set 双列集 ...
- httpclient使用head添加cookie
最近在使用接口时候,我使用get请求时,需要携带登录态,所以在get请求的时候我需要在head里面把cookie给加上,添加方式get和post完全不一样 Post方式添加cookie httpPos ...
- LA 3902 网络
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...