为何选择MySQL ......为什么?

时间:2014-05-13 13:41:06

标签: mysql

那么,使用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 |
+----+----------------------+-----------------------------+

为什么,为什么?为什么它会让我哭泣......?

1 个答案:

答案 0 :(得分:3)

文档中的所有内容都是:

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

特别是在行

  

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

因此,所有比较都是浮点比较,因此非常有意义。

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