mysql - 为什么是 MySQL ... 为什么?

标签 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 |
+----+----------------------+-----------------------------+

为什么,哦,为什么?为什么它让我哭得这么......?

最佳答案

文档里都有:

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

尤其是行

In all other cases, the arguments are compared as floating-point (real) numbers.

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

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

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

相关文章:

php - 如何使用 Zend_Db 有效地读取大量行?

php mysql连接困惑

php - 将 MYSQL phpmyadmin 导出到 Excel

mysql - 获取数组中表中最近30天当天的记录总数

sql - MYSQL - 一起使用 AVG() 和 DISTINCT

java - Selenium webdriver 线程 sleep 未按预期工作

php - 是否有一个 "automated"过程可以在整个代码中更改 SQL 列名称?

mysql - SQL - 将另一个表的值插入到多个列中并将它们嵌入到预定义的值中

php - 过期的链接

php - org.json.JSONException : Value DB of type java. lang.String 无法转换为 JSONObject