mysql 字符大于0,在MySQL中将字符串与“大于”和“小于”比较是安全的吗?

MySQL (5.1.41-3ubuntu12.10-log) seems to give predictable results on string comparison using > (greater than) and < (less than):

select "a" > "a", "a" > "b", "b" > "a", "ab" > "aa", "ab" > "aabbbb";

+-----------+-----------+-----------+-------------+-----------------+

| "a" > "a" | "a" > "b" | "b" > "a" | "ab" > "aa" | "ab" > "aabbbb" |

+-----------+-----------+-----------+-------------+-----------------+

| 0 | 0 | 1 | 1 | 1 |

+-----------+-----------+-----------+-------------+-----------------+

and also seems to make use of keys:

explain select productcode from products where productcode < 'no';

+----+-------------+----------+-------+-----------------+------+---------+------+------+--------------------------+

| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |

+----+-------------+----------+-------+-----------------+------+---------+------+------+--------------------------+

| 1 | SIMPLE | products | range | productcode,ppp | ppp | 34 | NULL | 432 | Using where; Using index |

+----+-------------+----------+-------+-----------------+------+---------+------+------+--------------------------+

This doesn't seem to be documented - is it a dependable cross-platform feature?

解决方案

I think there are some gotchas, you can have a look at documentation here for some details :

If your fields have null values too, you should also take a look at null-safe comparision operator:

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_equal-to

example :

mysql> select "a" > "a ", "A" > "a" , "aB" > "ab" , "a" >= NULL , "a" <=> NULL ;

+------------+-----------+--------------+-------------+--------------+

| "a" > "a " | "A" > "a" | "aB" > "ab" | "a" >= NULL | "a" <=> NULL |

+------------+-----------+--------------+-------------+--------------+

| 0 | 0 | 0 | NULL | 0 |

+------------+-----------+--------------+-------------+--------------+

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值