发现数据不一致,生产环境的更唬人一些,列中并没有存储0,而都是字母或纯数字,当我执行上述两个SQL的时候,发现结果差了非常多,还爆出了很多的warnings。
| Warning | 1292 | Truncated incorrect DOUBLE value: 'XXX' |
那么我想知道刚刚的误操作到底是不是生效了呢,为什么会出现差个引号结果就差这么多呢?
分析Part1:构建数据
root@127.0.0.1 (helei)> insert into helei3 values('zz','zz');
root@127.0.0.1 (helei)> select * from helei3;
+------+------+
| a | b |
+------+------+
| 1 | a |
| 0 | b |
| 0 | c |
| zz | zz |
+------+------+
4 rows in set (0.00 sec)
Part2:查询对比
那么这时我们执行一条查询会有两种结果
root@127.0.0.1 (helei)> select * from helei3 where a='0';
+------+------+
| a | b |
+------+------+
| 0 | b |
| 0 | c |
+------+------+
2 rows in set (0.00 sec)
root@127.0.0.1 (helei)> select * from helei3 where a=0;
+------+------+
| a | b |
+------+------+
| 0 | b |
| 0 | c |
| zz | zz |
+------+------+
3 rows in set (0.00 sec)
这是为什么呢?
Part3:root cause
root@127.0.0.1 (helei)> select 'zz'=0;
+--------+
| 'zz'=0 |
+--------+
| 1 |
+--------+
1 row in set, 1 warning (0.00 sec)
root@127.0.0.1 (helei)> select 'zz3'=0;
+---------+
| 'zz3'=0 |
+---------+
| 1 |
+---------+
1 row in set, 1 warning (0.00 sec)
root@127.0.0.1 (helei)> select '3'=0;
+-------+
| '3'=0 |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)
可以看出,当包含字母的时候,mysql认为=0是真,并抛出warning。
root@127.0.0.1 (helei)> show warnings;
+---------+------+----------------------------------------+
| Level | Code | Message |
+---------+------+----------------------------------------+
| Warning | 1292 | Truncated incorrect DOUBLE value: 'zz' |
+---------+------+----------------------------------------+
1 row in set (0.00 sec)
Part4:MySQL Doc