您的位置:首页 > 数据库 > MySQL

MySQL 隐式类型转换

2021-04-12 00:13 896 查看



create table t_base_user(    
oid bigint(20) not null primary key auto_increment,    
name varchar(30) null comment "name",    
email varchar(30) null comment "email",    
age int null comment "age",    
telephone varchar(30) null comment "telephone",    
status tinyint(4) null comment "0 无效 1 有效",    
created_at datetime null default now() comment "创建时间",    
updated_at datetime null default now() comment "修改时间"  );  
   
### 新建索引  
alter table t_base_user add index idx_oid(oid);  
alter table t_base_user add index idx_email(email);  
alter table t_base_user add index idx_name(name);  
alter table t_base_user add index idx_telephone(telephone);  
alter table t_base_user add index idx_telephone_email(telephone,email);  
alter table t_base_user add index idx_id_name(oid,name);  


alter table t_base_user add index idx_id_created_at(oid,created_at);  
alter table t_base_user add unique index idx_id_updated_at(oid,updated_at);  
alter table t_base_user add unique index idx_oid2(oid);  






   
### 新增记录:  
INSERT INTO `t_base_user` (`name`, `email`, `age`, `telephone`, `status`, `created_at`, `updated_at`)  
VALUES ('111111', 'andytohome@gmail.com', '111', '12345678901', '1', now(),now());  


insert into t_base_user(name,email,age,telephone,status) select name,email,age,telephone,status from t_base_user;  




### 查询  
explain select * from t_base_user where telephone=12345678901;  


explain select * from t_base_user where telephone='12345678901';  


explain select * from t_base_user where telephone=cast(12345678901 as char);  




explain select * from t_base_user where telephone=cast(12345678901 as char  charset latin1);  




explain select * from t_base_user where oid='1';  
explain select * from t_base_user where oid=1;  






--------------------------------------------- Oracle  
create table t_base_user(    
oid NUMBER(20) not null primary KEY ,    
name VARCHAR2(30)  ,    
email VARCHAR2(30) ,     
telephone VARCHAR2(30) );  




CREATE INDEX idx_telephone ON  t_base_user(telephone);  


INSERT INTO t_base_user(oid, name,email,telephone)  
VALUES ('111111', 'lhrbest@gmail.com', '111', '12345678901');  


set linesize 1000  
set autot on  
select * from t_base_user where telephone=12345678901;  
select * from t_base_user where telephone='12345678901';  








---------------------------------------------------------------------------------  
mysql> explain select * from t_base_user where telephone=12345678901;  
+----+-------------+-------------+------------+------+-----------------------------------+------+---------+------+--------+----------+-------------+  
| id | select_type | table       | partitions | type | possible_keys                     | key  | key_len | ref  | rows   | filtered | Extra       |  
+----+-------------+-------------+------------+------+-----------------------------------+------+---------+------+--------+----------+-------------+  
|  1 | SIMPLE      | t_base_user | NULL       | ALL  | idx_telephone,idx_telephone_email | NULL | NULL    | NULL | 521550 |    10.00 | Using where |  
+----+-------------+-------------+------------+------+-----------------------------------+------+---------+------+--------+----------+-------------+  
1 row in set, 5 warnings (0.00 sec)  


mysql> show warnings;  
+---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Level   | Code | Message                                                                                                                                                                                                                                                                                                                                                                                                                                                              |  
+---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Warning | 1739 | Cannot use ref access on index 'idx_telephone' due to type or collation conversion on field 'telephone'                                                                                                                                                                                                                                                                                                                                                              |  
| Warning | 1739 | Cannot use ref access on index 'idx_telephone_email' due to type or collation conversion on field 'telephone'                                                                                                                                                                                                                                                                                                                                                        |  
| Warning | 1739 | Cannot use range access on index 'idx_telephone' due to type or collation conversion on field 'telephone'                                                                                                                                                                                                                                                                                                                                                            |  
| Warning | 1739 | Cannot use range access on index 'idx_telephone_email' due to type or collation conversion on field 'telephone'                                                                                                                                                                                                                                                                                                                                                      |  
| Note    | 1003 | /* select#1 */ select `lhrdb`.`t_base_user`.`oid` AS `oid`,`lhrdb`.`t_base_user`.`name` AS `name`,`lhrdb`.`t_base_user`.`email` AS `email`,`lhrdb`.`t_base_user`.`age` AS `age`,`lhrdb`.`t_base_user`.`telephone` AS `telephone`,`lhrdb`.`t_base_user`.`status` AS `status`,`lhrdb`.`t_base_user`.`created_at` AS `created_at`,`lhrdb`.`t_base_user`.`updated_at` AS `updated_at` from `lhrdb`.`t_base_user` where (`lhrdb`.`t_base_user`.`telephone` = 12345678901) |  
+---------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
5 rows in set (0.00 sec)  


mysql> explain select * from t_base_user where telephone='12345678901';  
+----+-------------+-------------+------------+------+-----------------------------------+---------------+---------+-------+--------+----------+-------+  
| id | select_type | table       | partitions | type | possible_keys                     | key           | key_len | ref   | rows   | filtered | Extra |  
+----+-------------+-------------+------------+------+-----------------------------------+---------------+---------+-------+--------+----------+-------+  
|  1 | SIMPLE      | t_base_user | NULL       | ref  | idx_telephone,idx_telephone_email | idx_telephone | 33      | const | 260775 |   100.00 | NULL  |  
+----+-------------+-------------+------------+------+-----------------------------------+---------------+---------+-------+--------+----------+-------+  
1 row in set, 1 warning (0.00 sec)  


mysql>   
mysql> show warnings;  
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Level | Code | Message                                                                                                                                                                                                                                                                                                                                                                                                                                                                |  
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Note  | 1003 | /* select#1 */ select `lhrdb`.`t_base_user`.`oid` AS `oid`,`lhrdb`.`t_base_user`.`name` AS `name`,`lhrdb`.`t_base_user`.`email` AS `email`,`lhrdb`.`t_base_user`.`age` AS `age`,`lhrdb`.`t_base_user`.`telephone` AS `telephone`,`lhrdb`.`t_base_user`.`status` AS `status`,`lhrdb`.`t_base_user`.`created_at` AS `created_at`,`lhrdb`.`t_base_user`.`updated_at` AS `updated_at` from `lhrdb`.`t_base_user` where (`lhrdb`.`t_base_user`.`telephone` = '12345678901') |  
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
1 row in set (0.00 sec)  


mysql> explain select * from t_base_user where telephone=cast(12345678901 as char);  
+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+  
| id | select_type | table       | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |  
+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+  
|  1 | SIMPLE      | t_base_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 521550 |   100.00 | Using where |  
+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+  
1 row in set, 1 warning (0.00 sec)  


mysql>   
mysql>  show warnings;  
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Level | Code | Message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |  
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Note  | 1003 | /* select#1 */ select `lhrdb`.`t_base_user`.`oid` AS `oid`,`lhrdb`.`t_base_user`.`name` AS `name`,`lhrdb`.`t_base_user`.`email` AS `email`,`lhrdb`.`t_base_user`.`age` AS `age`,`lhrdb`.`t_base_user`.`telephone` AS `telephone`,`lhrdb`.`t_base_user`.`status` AS `status`,`lhrdb`.`t_base_user`.`created_at` AS `created_at`,`lhrdb`.`t_base_user`.`updated_at` AS `updated_at` from `lhrdb`.`t_base_user` where (convert(`lhrdb`.`t_base_user`.`telephone` using utf8) = (cast(12345678901 as char charset utf8))) |  
+-------+------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
1 row in set (0.00 sec)  




mysql>  explain select * from `lhrdb`.`t_base_user` where (convert(`lhrdb`.`t_base_user`.`telephone` using utf8) = cast(12345678901 as char charset utf8));  
+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+  
| id | select_type | table       | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |  
+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+  
|  1 | SIMPLE      | t_base_user | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 521550 |   100.00 | Using where |  
+----+-------------+-------------+------------+------+---------------+------+---------+------+--------+----------+-------------+  
1 row in set, 1 warning (0.00 sec)  


mysql> show full columns from lhrdb.t_base_user;  
+------------+-------------+-------------------+------+-----+-------------------+----------------+---------------------------------+-------------------+  
| Field      | Type        | Collation         | Null | Key | Default           | Extra          | Privileges                      | Comment           |  
+------------+-------------+-------------------+------+-----+-------------------+----------------+---------------------------------+-------------------+  
| oid        | bigint(20)  | NULL              | NO   | PRI | NULL              | auto_increment | select,insert,update,references |                   |  
| name       | varchar(30) | latin1_swedish_ci | YES  | MUL | NULL              |                | select,insert,update,references | name              |  
| email      | varchar(30) | latin1_swedish_ci | YES  | MUL | NULL              |                | select,insert,update,references | email             |  
| age        | int(11)     | NULL              | YES  |     | NULL              |                | select,insert,update,references | age               |  
| telephone  | varchar(30) | latin1_swedish_ci | YES  | MUL | NULL              |                | select,insert,update,references | telephone         |  
| status     | tinyint(4)  | NULL              | YES  |     | NULL              |                | select,insert,update,references | 0 无效 1 有效     |  
| created_at | datetime    | NULL              | YES  |     | CURRENT_TIMESTAMP |                | select,insert,update,references | 创建时间          |  
| updated_at | datetime    | NULL              | YES  |     | CURRENT_TIMESTAMP |                | select,insert,update,references | 修改时间          |  
+------------+-------------+-------------------+------+-----+-------------------+----------------+---------------------------------+-------------------+  
8 rows in set (0.00 sec)  


mysql> show index from lhrdb.t_base_user;  
+-------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
| Table       | Non_unique | Key_name            | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |  
+-------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
| t_base_user |          0 | PRIMARY             |            1 | oid         | A         |      521550 |     NULL | NULL   |      | BTREE      |         |               |  
| t_base_user |          0 | idx_oid2            |            1 | oid         | A         |      521550 |     NULL | NULL   |      | BTREE      |         |               |  
| t_base_user |          0 | idx_id_updated_at   |            1 | oid         | A         |      521550 |     NULL | NULL   |      | BTREE      |         |               |  
| t_base_user |          0 | idx_id_updated_at   |            2 | updated_at  | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
| t_base_user |          1 | idx_email           |            1 | email       | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
| t_base_user |          1 | idx_name            |            1 | name        | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
| t_base_user |          1 | idx_telephone       |            1 | telephone   | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
| t_base_user |          1 | idx_oid             |            1 | oid         | A         |      521550 |     NULL | NULL   |      | BTREE      |         |               |  
| t_base_user |          1 | idx_id_name         |            1 | oid         | A         |      521550 |     NULL | NULL   |      | BTREE      |         |               |  
| t_base_user |          1 | idx_id_name         |            2 | name        | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
| t_base_user |          1 | idx_id_created_at   |            1 | oid         | A         |      521550 |     NULL | NULL   |      | BTREE      |         |               |  
| t_base_user |          1 | idx_id_created_at   |            2 | created_at  | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
| t_base_user |          1 | idx_telephone_email |            1 | telephone   | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
| t_base_user |          1 | idx_telephone_email |            2 | email       | A         |      521550 |     NULL | NULL   | YES  | BTREE      |         |               |  
+-------------+------------+---------------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+  
14 rows in set (0.00 sec)  


mysql> explain select * from t_base_user where telephone=cast(12345678901 as char  charset latin1);  
+----+-------------+-------------+------------+------+-----------------------------------+---------------+---------+-------+--------+----------+-------+  
| id | select_type | table       | partitions | type | possible_keys                     | key           | key_len | ref   | rows   | filtered | Extra |  
+----+-------------+-------------+------------+------+-----------------------------------+---------------+---------+-------+--------+----------+-------+  
|  1 | SIMPLE      | t_base_user | NULL       | ref  | idx_telephone,idx_telephone_email | idx_telephone | 33      | const | 260775 |   100.00 | NULL  |  
+----+-------------+-------------+------------+------+-----------------------------------+---------------+---------+-------+--------+----------+-------+  
1 row in set, 1 warning (0.00 sec)  


mysql> show warnings;  
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Level | Code | Message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           |  
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
| Note  | 1003 | /* select#1 */ select `lhrdb`.`t_base_user`.`oid` AS `oid`,`lhrdb`.`t_base_user`.`name` AS `name`,`lhrdb`.`t_base_user`.`email` AS `email`,`lhrdb`.`t_base_user`.`age` AS `age`,`lhrdb`.`t_base_user`.`telephone` AS `telephone`,`lhrdb`.`t_base_user`.`status` AS `status`,`lhrdb`.`t_base_user`.`created_at` AS `created_at`,`lhrdb`.`t_base_user`.`updated_at` AS `updated_at` from `lhrdb`.`t_base_user` where (`lhrdb`.`t_base_user`.`telephone` = cast(12345678901 as char charset latin1)) |  
+-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+  
1 row in set (0.00 sec)  



mysql> explain select * from t_base_user where oid='1';  
+----+-------------+-------------+------------+-------+--------------------------------------------------------------------------+---------+---------+-------+------+----------+-------+  
| id | select_type | table       | partitions | type  | possible_keys                                                            | key     | key_len | ref   | rows | filtered | Extra |  
+----+-------------+-------------+------------+-------+--------------------------------------------------------------------------+---------+---------+-------+------+----------+-------+  
|  1 | SIMPLE      | t_base_user | NULL       | const | PRIMARY,idx_oid2,idx_id_updated_at,idx_oid,idx_id_name,idx_id_created_at | PRIMARY | 8       | const |    1 |   100.00 | NULL  |  
+----+-------------+-------------+------------+-------+--------------------------------------------------------------------------+---------+---------+-------+------+----------+-------+  
1 row in set, 1 warning (0.00 sec)  


mysql> explain select * from t_base_user where oid=1;  
+----+-------------+-------------+------------+-------+--------------------------------------------------------------------------+---------+---------+-------+------+----------+-------+  
| id | select_type | table       | partitions | type  | possible_keys                                                            | key     | key_len | ref   | rows | filtered | Extra |  
+----+-------------+-------------+------------+-------+--------------------------------------------------------------------------+---------+---------+-------+------+----------+-------+  
|  1 | SIMPLE      | t_base_user | NULL       | const | PRIMARY,idx_oid2,idx_id_updated_at,idx_oid,idx_id_name,idx_id_created_at | PRIMARY | 8       | const |    1 |   100.00 | NULL  |  
+----+-------------+-------------+------------+-------+--------------------------------------------------------------------------+---------+---------+-------+------+----------+-------+  
1 row in set, 1 warning (0.00 sec)  


mysql>   
 






SYS@PROD1> set linesize 1000  
SYS@PROD1> set autot on  
SYS@PROD1> select * from t_base_user where telephone=12345678901;  


       OID NAME                           EMAIL                          TELEPHONE  
---------- ------------------------------ ------------------------------ ------------------------------  
    111111 lhrbest@gmail.com              111                            12345678901  




Execution Plan  
----------------------------------------------------------  


Plan hash value: 3483759010  


---------------------------------------------------------------------------------  
| Id  | Operation         | Name        | Rows  | Bytes | Cost (%CPU)| Time     |  
---------------------------------------------------------------------------------  
|   0 | SELECT STATEMENT  |             |     1 |    64 |     2   (0)| 00:00:01 |  
|*  1 |  TABLE ACCESS FULL| T_BASE_USER |     1 |    64 |     2   (0)| 00:00:01 |  
---------------------------------------------------------------------------------  


Predicate Information (identified by operation id):  
---------------------------------------------------  


   1 - filter(TO_NUMBER("TELEPHONE")=12345678901)  


Note  
-----  
   - dynamic sampling used for this statement (level=2)  
   - automatic DOP: Computed Degree of Parallelism is 1 because of parallel threshold  




Statistics  
----------------------------------------------------------  
          0  recursive calls  
          0  db block gets  
          3  consistent gets  
          0  physical reads  
          0  redo size  
        633  bytes sent via SQL*Net to client  
        419  bytes received via SQL*Net from client  
          2  SQL*Net roundtrips to/from client  
          0  sorts (memory)  
          0  sorts (disk)  
          1  rows processed  


SYS@PROD1> select * from t_base_user where telephone='12345678901';  


       OID NAME                           EMAIL                          TELEPHONE  
---------- ------------------------------ ------------------------------ ------------------------------  
    111111 lhrbest@gmail.com              111                            12345678901  




Execution Plan  
----------------------------------------------------------  
Plan hash value: 1190906832  


---------------------------------------------------------------------------------------------  
| Id  | Operation                   | Name          | Rows  | Bytes | Cost (%CPU)| Time     |  
---------------------------------------------------------------------------------------------  
|   0 | SELECT STATEMENT            |               |     1 |    64 |     1   (0)| 00:00:01 |  
|   1 |  TABLE ACCESS BY INDEX ROWID| T_BASE_USER   |     1 |    64 |     1   (0)| 00:00:01 |  
|*  2 |   INDEX RANGE SCAN          | IDX_TELEPHONE |     1 |       |     1   (0)| 00:00:01 |  
---------------------------------------------------------------------------------------------  


Predicate Information (identified by operation id):  
---------------------------------------------------  


   2 - access("TELEPHONE"='12345678901')  


Note  
-----  
   - dynamic sampling used for this statement (level=2)  
   - automatic DOP: Computed Degree of Parallelism is 1 because of parallel threshold  




Statistics  
----------------------------------------------------------  
         33  recursive calls  
          0  db block gets  
         11  consistent gets  
          2  physical reads  
          0  redo size  
        637  bytes sent via SQL*Net to client  
        419  bytes received via SQL*Net from client  
          2  SQL*Net roundtrips to/from client  
          0  sorts (memory)  
          0  sorts (disk)  
          1  rows processed  


SYS@PROD1>   



MySQL隐式转化整理

rollenholt       2016-05-06 16:04:42       浏览1031       评论0  

云数据库RDS

摘要: 前几天在微博上看到一篇文章:价值百万的 MySQL 的隐式类型转换感觉写的很不错,再加上自己之前也对MySQL的隐式转化这边并不是很清楚,所以就顺势整理了一下。希望对大家有所帮助。 当我们对不同类型的值进行比较的时候,为了使得这些数值「可比较」(也可以称为类型的兼容性),MySQL会做一些隐式转化(Implicit type conversion)。

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

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

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

 

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

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

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 则会匹配上。

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注入。

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

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

 

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

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

 

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

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)

 

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

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)

 

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

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

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

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了。

在看一个例子:

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.

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

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的时候就可能会多操作一些数据。所以应该加引号的地方别忘记了。

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

 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
  • 如果字符串以数字开头
  • 如果字符串中都是数字,那么转换为数字就是整个字符串对应的数字
  • 如果字符串中存在非数字,那么转换为的数字就是开头的那些数字对应的值

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

参考资料

  • http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html
  • https://blog.eood.cn/mysql_params
  • http://dev.mysql.com/doc/refman/5.7/en/type-conversion.html



前言:MySQL的隐式转换是什么样子的,什么时候会进行隐式转换,下面让我们来掀开MySQL隐式转换的面纱,下面我们通过一个例子来进行说明  



隐式转换的几种情况:

1、当不同类型的字段一起使用时,会发生隐式转换

root@localhost:mysql.sock  16:18:23 [tom]>SELECT 1+'1';
+-------+| 1+'1' |
+-------+|     2 |
+-------+1 row in set (0.00 sec)

root@localhost:mysql.sock  16:18:26 [tom]>SELECT CONCAT(2,' test');
+-------------------+| CONCAT(2,' test') |
+-------------------+| 2 test            |
+-------------------+1 row in set (0.00 sec)

 

或者使用函数显示或者隐式进行转换

root@localhost:mysql.sock  16:29:39 [tom]>SELECT 38.8, CAST(38.8 AS CHAR);
+------+--------------------+
| 38.8 | CAST(38.8 AS CHAR) |
+------+--------------------+
| 38.8 | 38.8               |
+------+--------------------+1 row in set (0.00 sec)root@localhost:mysql.sock  16:30:27 [tom]>SELECT 38.8, CONCAT(38.8);
+------+--------------+
| 38.8 | CONCAT(38.8) |
+------+--------------+
| 38.8 | 38.8         |
+------+--------------+1 row in set (0.00 sec)

 

2、下面是一些会发生隐式转换的规则

1、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.2、If both arguments in a comparison operation are strings, they are compared as strings.3、If both arguments are integers, they are compared as integers.4、Hexadecimal values are treated as binary strings if not compared to a number.5、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.
6、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.
7、In all other cases, the arguments are compared as floating-point (real) numbers.

 

3、下面是字符串转换成数字进行比较的

root@localhost:mysql.sock  17:23:38 [tom]>SELECT 1 > '6x';
+----------+| 1 > '6x' |
+----------+|        0 |
+----------+1 row in set, 1 warning (0.00 sec)

root@localhost:mysql.sock  17:23:41 [tom]>SELECT 7 > '6x';
+----------+| 7 > '6x' |
+----------+|        1 |
+----------+1 row in set, 1 warning (0.01 sec)

root@localhost:mysql.sock  17:23:48 [tom]>SELECT 0 > 'x6';
+----------+| 0 > 'x6' |
+----------+|        0 |
+----------+1 row in set, 1 warning (0.00 sec)

root@localhost:mysql.sock  17:23:55 [tom]>SELECT 0 = 'x6';
+----------+| 0 = 'x6' |
+----------+|        1 |
+----------+1 row in set, 1 warning (0.00 sec)

 

4、如果在sql中对一个字符串和数字比较,mysql不会使用索引

root@localhost:mysql.sock  17:24:03 [tom]>explain select * from test where name = 1;;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+| id | select_type | table | partitions | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | test  | NULL       | ALL  | name          | NULL | NULL    | NULL |    9 |    11.11 | Using where |+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------------+1 row in set, 3 warnings (0.00 sec)ERROR: No query specified

root@localhost:mysql.sock  17:25:42 [tom]>show create table test;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table                                                                                                                                                                                               |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| test  | CREATE TABLE `test` (
  `id` int(11) DEFAULT NULL,
  `age` int(11) DEFAULT NULL,
  `name` varchar(4) NOT NULL DEFAULT 'tom',
  KEY `name` (`name`),
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)

 

The reason for this is that there are many different strings that may convert to the value1, such as‘1’,‘ 1’, or ‘1a’

5、浮点数的比较是近似值,可能导致结果不准确,可能会有取舍

root@localhost:mysql.sock  17:30:19 [tom]>SELECT '18015376320243459'+0.0;
+-------------------------+| '18015376320243459'+0.0 |
+-------------------------+|    1.801537632024346e16 |
+-------------------------+1 row in set (0.00 sec)

 

浮点数与整数之间的转换,或者符号之间的转换最好使用函数CAST(),避免隐式转换


总结:

1、sql中的where条件禁止进行不同字段类型的比较

2、字符串类型和数字比较会转换成0然后进行比较

3、字符串转换成数字的时候是从最左边开始的(不是数字就是0,是数字就匹配最左数字,这就导致可能会多查询或者删除、更新数据,这个结果就比较悲催了)



小心MySQL的隐式类型转换陷阱

 Posted on 2016-05-05    |  In MySQL    |     |  Visitors 497  

1. 隐式类型转换实例

今天生产库上突然出现MySQL线程数告警,IOPS很高,实例会话里面出现许多类似下面的sql:(修改了相关字段和值)  


 


SELECT f_col3_id,f_qq1_id FROM d_dbname.t_tb1 WHERE f_col1_id=1226391 and f_col2_id=1244378 and  

f_qq1_id in (12345,23456,34567,45678,56789,67890,78901,89012,90123,901231,901232,901233)  


mysql>explain extended SELECT f_col3_id,f_qq1_id FROM d_dbname.t_tb1 use index(idx_corpid_qq1id) WHERE f_col1_id=   1226391       and    f_col2_id=   1244378       and    f_qq1_id in (   12345   ,   23456   ,   34567   ,   45678   ,   56789   ,   67890   ,   78901   ,   89012   ,   90123   ,   901231   ,   901232   ,   901233   )   ;      
用 explain 看了下扫描行数和索引选择情况:  

mysql>explain SELECT f_col3_id,f_qq1_id FROM d_dbname.t_tb1 WHERE f_col1_id=1226391  

and f_col2_id=1244378 and f_qq1_id in (12345,23456,34567,45678,56789,67890,78901,89012,90123,901231,901232,901233);  

+------+---------------+---------+--------+--------------------------------+---------------+------------+--------+--------+------------------------------------+  

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |  

+------+---------------+---------+--------+--------------------------------+---------------+------------+--------+--------+------------------------------------+  

| 1 | SIMPLE | t_tb1 | ref | uid_type_frid,idx_corpid_qq1id | uid_type_frid | 8 | const | 1386 | Using index condition; Using where |  

+------+---------------+---------+--------+--------------------------------+---------------+------------+--------+--------+------------------------------------+  

共返回 1 行记录,花费 11.52 ms.  



t_tb1 表上有个索引uid_type_frid(f_col2_id,f_type)、idx_corp_id_qq1id(f_col1_id,f_qq1_id),而且如果选择后者时,f_qq1_id的过滤效果应该很佳,但却选择了前者。当使用 hint use index(idx_corp_id_qq1id)时:  


 



+------+---------------+--------+--------+---------------------+------------------+------------+----------+-------------+------------------------------------+  

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |  

+------+---------------+--------+--------+---------------------+------------------+------------+----------+-------------+------------------------------------+  

| 1 | SIMPLE | t_tb1 | ref | idx_corpid_qq1id | idx_corpid_qq1id | 8 | const | 2375752 | Using index condition; Using where |  

+---- -+---------------+--------+--------+---------------------+------------------+------------+----------+-------------+------------------------------------+  

共返回 1 行记录,花费 17.48 ms.  

mysql>show warnings;  

+-----------------+----------------+-----------------------------------------------------------------------------------------------------------------------+  

| Level | Code | Message |  

+-----------------+----------------+-----------------------------------------------------------------------------------------------------------------------+  

| Warning | 1739 | Cannot use range access on index 'idx_corpid_qq1id' due to type or collation conversion on field 'f_qq1_id' |  

| Note | 1003 | /* select#1 */ select `d_dbname`.`t_tb1`.`f_col3_id` AS `f_col3_id`,`d_dbname`.`t_tb1`.`f_qq1_id` AS `f_qq1_id` from `d_dbname`.`t_tb1` USE INDEX (`idx_corpid_qq1id`) where |  

| | | ((`d_dbname`.`t_tb1`.`f_col2_id` = 1244378) and (`d_dbname`.`t_tb1`.`f_col1_id` = 1226391) and (`d_dbname`.`t_tb1`.`f_qq1_id` in |  

| | | (12345,23456,34567,45678,56789,67890,78901,89012,90123,901231,901232,901233))) |  

+-----------------+----------------+-----------------------------------------------------------------------------------------------------------------------+  

共返回 2 行记录,花费 10.81 ms.  


rows列达到200w行,但问题也发现了:select_type应该是 range 才对,key_len看出来只用到了idx_corpid_qq1id索引的第一列。上面explain使用了 extended,所以show warnings;可以很明确的看到 f_qq1_id 出现了隐式类型转换:f_qq1_id是varchar,而后面的比较值是整型。  


解决该问题就是避免出现隐式类型转换(implicit type conversion)带来的不可控:把f_qq1_id in的内容写成字符串:  


mysql>explain SELECT f_col3_id,f_qq1_id FROM d_dbname.t_tb1 WHERE f_col1_id=1226391 and f_col2_id=1244378 and  

f_qq1_id in ('12345','23456','34567','45678','56789','67890','78901','89012','90123','901231');  

+-------+---------------+--------+---------+--------------------------------+------------------+-------------+---------+---------+------------------------------------+  

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |  

+-------+---------------+--------+---------+--------------------------------+------------------+-------------+---------+---------+------------------------------------+  

| 1 | SIMPLE | t_tb1 | range | uid_type_frid,idx_corpid_qq1id | idx_corpid_qq1id | 70 | | 40 | Using index condition; Using where |  

+-------+---------------+--------+---------+--------------------------------+------------------+-------------+---------+---------+------------------------------------+  

共返回 1 行记录,花费 12.41 ms.  



类似的还出现过一例:扫描行数从1386减少为40。  


SELECT count(0) FROM d_dbname.t_tb2 where f_col1_id= '1931231' AND f_phone in(098890);  

| Warning | 1292 | Truncated incorrect DOUBLE value: '1512-98464356'  



借这个机会,系统的来看一下mysql中的隐式类型转换。优化后直接从扫描rows 100w行降为1。  


2. mysql隐式转换规则

2.1 规则

下面来分析一下   隐式转换的规则   :  


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



mysql> select 11 + '11', 11 + 'aa', 'a1' + 'bb', 11 + '0.01a';    

+-----------+-----------+-------------+--------------+  

| 11 + '11' | 11 + 'aa' | 'a1' + 'bb' | 11 + '0.01a' |  

+-----------+-----------+-------------+--------------+  

| 22 | 11 | 0 | 11.01 |  

+-----------+-----------+-------------+--------------+  

1 row in set, 4 warnings (0.00 sec)  

mysql> show warnings;  

+---------+------+-------------------------------------------+  

| Level | Code | Message |  

+---------+------+-------------------------------------------+  

| Warning | 1292 | Truncated incorrect DOUBLE value: 'aa' |  

| Warning | 1292 | Truncated incorrect DOUBLE value: 'a1' |  

| Warning | 1292 | Truncated incorrect DOUBLE value: 'bb' |  

| Warning | 1292 | Truncated incorrect DOUBLE value: '0.01a' |  

+---------+------+-------------------------------------------+  

4 rows in set (0.00 sec)  

mysql> select '11a' = 11, '11.0' = 11, '11.0' = '11', NULL = 1;  

+------------+-------------+---------------+----------+  

| '11a' = 11 | '11.0' = 11 | '11.0' = '11' | NULL = 1 |  

+------------+-------------+---------------+----------+  

| 1 | 1 | 0 | NULL |  

+------------+-------------+---------------+----------+  

1 row in set, 1 warning (0.01 sec)  


0.01a转成double型也是被截断成0.01,所以11 + '0.01a' = 11.01。上面可以看出11 + 'aa',由于操作符两边的类型不一样且符合第g条,aa要被转换成浮点型小数,然而转换失败(字母被截断),可以认为转成了 0,整数11被转成浮点型还是它自己,所以11 + 'aa' = 11。  


等式比较也说明了这一点,'11a'和'11.0'转换后都等于 11,这也正是文章开头实例为什么没走索引的原因: varchar型的f_qq1_id,转换成浮点型比较时,等于 12345 的情况有无数种如12345a、12345.b等待,MySQL优化器无法确定索引是否更有效,所以选择了其它方案。  


但并不是只要出现隐式类型转换,就会引起上面类似的性能问题,最终是要看转换后能否有效选择索引。像f_id = '654321'、f_mtime between '2016-05-01 00:00:00' and '2016-05-04 23:59:59'就不会影响索引选择,因为前者f_id是整型,即使与后面的字符串型数字转换成double比较,依然能根据double确定f_id的值,索引依然有效。后者是因为符合第e条,只是右边的常量做了转换。  


开发人员可能都只要存在这么一个隐式类型转换的坑,但却又经常不注意,所以干脆无需记住那么多规则,该什么类型就与什么类型比较。  


2.2 隐式类型转换的安全问题

implicit type conversion 不仅可能引起性能问题,还有可能产生安全问题。  


mysql> desc t_account;  

+-----------+-------------+------+-----+---------+----------------+  

| Field | Type | Null | Key | Default | Extra |  

+-----------+-------------+------+-----+---------+----------------+  

| fid | int(11) | NO | PRI | NULL | auto_increment |  

| fname | varchar(20) | YES | | NULL | |  

| fpassword | varchar(50) | YES | | NULL | |  

+-----------+-------------+------+-----+---------+----------------+  

mysql> select * from t_account;  

+-----+-----------+-------------+  

| fid | fname | fpassword |  

+-----+-----------+-------------+  

| 1 | xiaoming | p_xiaoming |  

| 2 | xiaoming1 | p_xiaoming1 |  

+-----+-----------+-------------+  

假如应用前端没有WAF防护,那么下面的sql很容易注入:  

mysql> select * from t_account where fname='A' ;  

fname传入 A' OR 1='1    

mysql> select * from t_account where fname='A' OR 1='1';  


***者更聪明一点: fname传入 A'+'B ,fpassword传入 ccc'+0 :  



mysql> select * from t_account where fname='A'+'B' and fpassword='ccc'+0;  

+-----+-----------+-------------+  

| fid | fname | fpassword |  

+-----+-----------+-------------+  

| 1 | xiaoming | p_xiaoming |  

| 2 | xiaoming1 | p_xiaoming1 |  

+-----+-----------+-------------+  

2 rows in set, 7 warnings (0.00 sec)  



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: