Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform case insensitive ORDER BY in mysql?

Tags:

mysql

I want to perform case-insensitive ORDER BY in MySQL. I have the data in my database like A, C, b, e, D etc

I'm getting the result as A, C, D, b, e

But, I want the result as A, b, C, D, e

How can I get that?

like image 944
Manikandan C Avatar asked Jan 10 '18 13:01

Manikandan C


People also ask

How do I create a case-insensitive query in MySQL?

If you want case-insensitive distinct, you need to use UPPER() or LOWER().

Is MySQL ORDER BY case-sensitive?

MySQL by default will make the ORDER BY option in queries case sensitive. This means that rows with the same starting letters (but different case) may be ordered non-alphabetically.

Is SQL ORDER BY case-sensitive?

SQL Server is, by default, case insensitive; however, it is possible to create a case-sensitive SQL Server database and even to make specific table columns case sensitive. The way to determine if a database or database object is to check its "COLLATION" property and look for "CI" or "CS" in the result.

Is MySQL case-insensitive by default?

Table names are stored in lowercase on disk and name comparisons are not case-sensitive. MySQL converts all table names to lowercase on storage and lookup. This behavior also applies to database names and table aliases.


1 Answers

Choose a case-insensitive collation

select * from your_table
order by your_column COLLATE utf8_general_ci

That way indexes still work and the query is fast.

like image 188
juergen d Avatar answered Nov 15 '22 20:11

juergen d