How do MIN () and MAX () work on CHAR / VARCHAR strings in MySQL?

I played with the world database (InnoDB) found here to get more information about MySQL.

I entered the following basic query:

SELECT COUNT(Name), MIN(Name), MAX(Name) FROM Country GROUP BY Continent 

It seems that the MIN () and MAX () methods work in CHAR strings in the title in alphabetical order, where A is the smallest value, and Z the largest, etc.

Can someone explain what happens behind the scenes and what values ​​are built to sort them this way? What will happen to strings of both alphabetic characters and integers or special characters?

Insight is greatly appreciated.

+7
mysql
source share
1 answer

MySQL string comparisons, technically called sorting, are locale-specific. I suggest reading sections 10.1.1 on general sorting; 10.1.2 on sorting in MySQL; and 10.1.7 on sorting problems. At http://collation-charts.org/ you can find out more about individual sorts, for example. which is called latin1_general_ci (where _ci means case insensitive). The sort used when you call MAX and MIN should be the collation of the column in question, unless you specify another sort using wording of type MAX(Name COLLATE latin1_german2_ci) .

+1
source share

All Articles