Skip to content
gqlxj1987's Blog
Go back

Mysql transfer implicit

Edit page

原文链接

Implicit type conversion

一些规则:

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)

可以登录系统

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

主要是针对字符串部分

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

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)

这样当进行select,update或者delete的时候就可能会多操作一些数据。所以应该加引号的地方别忘记了。

当把字符串转为数字的时候,其实是从左边开始处理的。


Edit page
Share this post on:

Previous Post
MemSQL intro
Next Post
自取其辱