Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex mysql ORDER BY

Tags:

sql

mysql

Doing the following query:

SELECT title FROM table ORDER BY title

gives me:

"Hello"
"Zebra"
Apple
Beta
Cactus

How would I ORDER BY the first alphabetical character, in order to get:

Apple
Beta
Cactus
"Hello"
"Zebra"

?

like image 743
David542 Avatar asked Jul 31 '12 10:07

David542


People also ask

How do I ORDER BY ascending in MySQL?

The MySQL ORDER BY Keyword The ORDER BY keyword is used to sort the result-set in ascending or descending order. The ORDER BY keyword sorts the records in ascending order by default. To sort the records in descending order, use the DESC keyword.

Does order matter in MySQL?

Whereas if the telephone book were organized by first name then by last name, you'd find all the Johns together, then within the Johns, all the 'S' last names would be grouped together. So the order of columns in a multi-column index definitely matters. One type of query may need a certain column order for the index.

What is using Filesort in MySQL?

In MySQL, filesort is the catch-all algorithm for producing sorted results for ORDER-BY or GROUP-BY queries. MySQL has two algorithms for filesort, both the original and the modified algorithms are described in the user manual.


1 Answers

You will have to remove quotes before sorting data. You can easily remove them using TRIM function in MySQL as:

SELECT title 
FROM table 
ORDER BY TRIM(BOTH '"' FROM title);
like image 192
Omesh Avatar answered Oct 16 '22 22:10

Omesh