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
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?
Insight much appreciated.
The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used.
Varchar in MySQL is a data type used for storing text whose length can have a maximum of 65535 characters. The varchar columns in the table are of variable length string that can hold either numeric or character or both. This data type is capable of storing only 255 characters before version 5.0.
MIN(expression) and MAX(expression) find the minimum and maximum value (string, datetime, or numeric) in a set of rows. DISTINCT or ALL may be used with these functions, but they do not affect the result. MIN and MAX are supported by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL.
MySQL MIN() and MAX() FunctionsThe MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.
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)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With