那么,对于mysql,为什么彼此不相等的事物却彼此相等?例如,为什么...

mysql> SELECT '3' = 3;
+---------+
| '3' = 3 |
+---------+
|       1 |
+---------+


只是,为什么?

更重要的是...

mysql> SELECT 0 = '';
+--------+
| 0 = '' |
+--------+
|      1 |
+--------+


但为什么?

还...

mysql> SELECT '3x' into @foo;
Query OK, 1 row affected (0.00 sec)

mysql> SELECT @foo, CAST(@foo as signed), @foo = CAST(@foo as signed);
+------+----------------------+-----------------------------+
| @foo | CAST(@foo as signed) | @foo = CAST(@foo as signed) |
+------+----------------------+-----------------------------+
| 3x   |                    3 |                           1 |
+------+----------------------+-----------------------------+


亲爱的上帝,为什么?

但更糟糕的是...

mysql> SELECT '3x', CAST('3z' as signed), '3x' = CAST('3z' as signed);
+----+----------------------+-----------------------------+
| 3x | CAST('3z' as signed) | '3x' = CAST('3z' as signed) |
+----+----------------------+-----------------------------+
| 3x |                    3 |                           1 |
+----+----------------------+-----------------------------+


为什么,为什么呢?为什么它让我哭泣……?

最佳答案

所有这些都在文档中:

http://dev.mysql.com/doc/refman/5.1/en/type-conversion.html

特别是在生产线上


  在所有其他情况下,将参数作为浮点数(实数)进行比较。


因此,您所有的比较都是浮点比较,因此非常合理。

有人会说自动类型转换是否完全有意义(是否可以将'1'与1进行比较?)...

关于mysql - 为什么是MySQL……为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23632896/

10-16 01:12