MIN()和MAX()如何在MySQL中的CHAR/VARCHAR字符串上工作? [英] How do MIN() and MAX() work on CHAR/VARCHAR strings in MySQL?
问题描述
我一直在在这里找到世界数据库(InnoDB).以获得对MySQL的更多了解.
I've been playing with the world database (InnoDB) found here to gain more insight into MySQL.
我输入了以下基本查询:
I entered the following basic query:
SELECT COUNT(Name), MIN(Name), MAX(Name)
FROM Country
GROUP BY Continent
在Name中的CHAR字符串上,MIN()和MAX()的工作方式似乎是按字母顺序排列的,其中A是最小的值,Z是最大的,依此类推.
It seems that the way MIN() and MAX() work on the CHAR strings in Name is by alphabetical order, where A is the smallest value and Z is the greatest and so forth.
任何人都可以解释幕后发生的事情以及为它们分配什么样的值字符串以便以这种方式进行排序吗?既有字母字符又有整数或特殊字符的字符串会发生什么情况?
Can anyone explain what is going on behind the scenes and what values strings are being assigned for them to be sorted in such a way? What would happen with strings of both alphabetic characters and integers, or special characters?
非常感谢见识.
推荐答案
MySQL字符串比较(技术上称为 collation )是与语言环境相关的.我建议阅读 10.1.1 部分, 10.1.2 关于MySQL和 10.1.7 .在 http://collation-charts.org/上,您可以找到有关各个归类的详细信息,例如称为 latin1_general_ci
的文件(其中_ci
表示不区分大小写).除非您使用
MySQL string comparison, technically called collation, is locale-dependent. I suggest reading sections 10.1.1 on collation in general, 10.1.2 on collation in MySQL and 10.1.7 about collation issues. On http://collation-charts.org/ you can find out details about the individual collations, e.g. the one called latin1_general_ci
(where _ci
stands for case insensitive). The collation used when you call MAX
and MIN
should be the collation of the column in question, unless you specify a different collation using a formulation like MAX(Name COLLATE latin1_german2_ci)
.
这篇关于MIN()和MAX()如何在MySQL中的CHAR/VARCHAR字符串上工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!