Is it safe to compare strings with 'greater than' and 'less than' in MySQL?

試著忘記壹切 提交于 2019-11-29 10:35:48
DhruvPathak

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

http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html

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

These comparisons are common. I'm certain that comparing strings by ascii value or some other encoding like that is supported cross-platform. Sorry I don't have any resources to back it up. That's probably the way it compares strings (for sorting and such) internally. I would expect that to be a dominant feature.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!