日期:2014-05-16  浏览次数:20828 次

binary
mysql> select '50' >= 50;
+------------+
| '50' >= 50 |
+------------+
| 1 | 
+------------+
1 row in set (0.00 sec)
mysql> select '50' >= binary 50;
+-------------------+
| '50' >= binary 50 |
+-------------------+
| 1 | 
+-------------------+
1 row in set (0.00 sec)
mysql> select '6' > 50;
+----------+
| '6' > 50 |
+----------+
| 0 | 
+----------+
1 row in set (0.00 sec)
mysql> select '6' > binary 50;
+-----------------+
| '6' > binary 50 |
+-----------------+
| 1 | 
+-----------------+
1 row in set (0.00 sec)

select '6' > binary 50; 这个为什么?

------解决方案--------------------
binary 50 

说明了 50 将是个字符串! 这样其实就是做的字符串比较。
------解决方案--------------------
是这样的,当不带binary时,默认将后者转化成前者来比较。
如:
mysql> select '50' = 50;
+-----------+
| '50' = 50 |
+-----------+
| 1 | 
+-----------+

但是如果带了binary时,那将会把前者转化成binary值进行比较
mysql> select '6' > binary 50
-> ;
+-----------------+
| '6' > binary 50 |
+-----------------+
| 1 | 
+-----------------+
1 row in set (0.00 sec)

这相当于比较select 54 > 50
'6'的二进制表示是36,即十进制的54