6.7.1 Problem
String sorts are case sensitive when you don't want them to be, or vice versa.
6.7.2 Solution
Alter the case sensitivity of the sorted values.
6.7.3 Discussion
Chapter 4 discusses the fact that binary strings are case sensitive in comparisons, whereas non-binary strings are not. This property carries over into string sorting as well: ORDER BY produces lexical sorts that are case sensitive for binary strings and not case sensitive for non-binary strings. The following table textblob_val contains a TEXT column tstr and a BLOB column bstr that serve to demonstrate this:
mysql> SELECT * FROM textblob_val; +------+------+ | tstr | bstr | +------+------+ | aaa | aaa | | AAA | AAA | | bbb | bbb | | BBB | BBB | +------+------+
Both columns contain the same values. But they produce different sort results, because TEXT columns are not case sensitive and BLOB columns are:
mysql> SELECT tstr FROM textblob_val ORDER BY tstr; +------+ | tstr | +------+ | aaa | | AAA | | bbb | | BBB | +------+ mysql> SELECT bstr FROM textblob_val ORDER BY bstr; +------+ | bstr | +------+ | AAA | | BBB | | aaa | | bbb | +------+
To control case sensitivity in ORDER BY clauses, use the techniques discussed in Chapter 4 for affecting string comparisons. To perform a case-sensitive sort for strings that are not case sensitive (such as those in the tstr column) cast the sort column to binary-string form using the BINARY keyword:
mysql> SELECT tstr FROM textblob_val ORDER BY BINARY tstr; +------+ | tstr | +------+ | AAA | | BBB | | aaa | | bbb | +------+
Another possibility is to convert the output column to binary and sort that:
mysql> SELECT BINARY tstr FROM textblob_val ORDER BY 1; +-------------+ | BINARY tstr | +-------------+ | AAA | | BBB | | aaa | | bbb | +-------------+
You can also use the CAST( ) function that is available as of MySQL 4.0.2:
mysql> SELECT tstr FROM textblob_val ORDER BY CAST(tstr AS BINARY); +------+ | tstr | +------+ | AAA | | BBB | | aaa | | bbb | +------+
The complementary operation is to sort binary strings in non-case-sensitive fashion. To do this, convert the values to uppercase or lowercase with UPPER( ) or LOWER( ):
mysql> SELECT bstr FROM textblob_val ORDER BY UPPER(bstr); +------+ | bstr | +------+ | aaa | | AAA | | bbb | | BBB | +------+
Alternatively, you can convert the output column and sort thatbut doing so affects the displayed values, possibly in an undesirable way:
mysql> SELECT UPPER(bstr) FROM textblob_val ORDER BY 1; +-------------+ | UPPER(bstr) | +-------------+ | AAA | | AAA | | BBB | | BBB | +-------------+
Using the mysql Client Program
Writing MySQL-Based Programs
Record Selection Techniques
Working with Strings
Working with Dates and Times
Sorting Query Results
Generating Summaries
Modifying Tables with ALTER TABLE
Obtaining and Using Metadata
Importing and Exporting Data
Generating and Using Sequences
Using Multiple Tables
Statistical Techniques
Handling Duplicates
Performing Transactions
Introduction to MySQL on the Web
Incorporating Query Resultsinto Web Pages
Processing Web Input with MySQL
Using MySQL-Based Web Session Management
Appendix A. Obtaining MySQL Software
Appendix B. JSP and Tomcat Primer
Appendix C. References