将字符串与“大于”进行比较是否安全?和“小于”在 MySQL 中?
MySQL(5.1.41-3ubuntu12.10-log)似乎使用 > 给出了可预测的字符串比较结果。 (大于)和 < (小于):
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 |
+-----------+-----------+-----------+-------------+-----------------+
并且似乎还使用了键:
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 |
+----+-------------+----------+-------+-----------------+------+---------+------+------+--------------------------+
这似乎没有记录 - 这是一个可靠的跨平台功能吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这些比较很常见。我确信通过 ascii 值或类似的其他编码来比较字符串是跨平台支持的。抱歉,我没有任何资源来支持它。这可能就是它在内部比较字符串(用于排序等)的方式。我希望这是一个主要特征。
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.
我认为有一些问题,您可以查看此处的文档以了解一些详细信息:
http://dev.mysql.com/doc/refman/5.5/en/comparison-operators.html
如果您的字段也有空值,您还应该看看空安全比较操作员:
http://dev.mysql.com/doc /refman/5.0/en/comparison-operators.html#operator_equal-to
示例:
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 类型转换 文档关于“比较操作如何进行转换”:
作为字符串比较是指所涉及的值/列的排序规则的排序算法。
关于涉及的多个排序规则
如果比较具有不同排序规则的两个操作数(列、文字...)会怎样?请参阅 MySQL 排序规则强制。
In short: It is predictable because of the sorting algorithm of the involved collation(s).
About comparison in general
From the MySQL type conversion docs on "how conversion occurs for comparison operations":
Compared as string refers to the sorting algorithm of the collation(s) of the involved values / columns.
About multiple collations involved
What if two operands (columns, literals ...) with different collations are compared? See MySQL collation coersion.