Sort MySQL results alphabetically, but with numbers last - mysql

Often, sorting is done with symbols sorted to the top, like 0 or * or &. This is the default way that mysql sorts; numbers and symbols and then A-Z. However, that makes the often ugliest or most badly formatted results float to the top (e.g. a result of ##$#3423 or 8 inch or &amp).
So I'd like to do a modified form of that, letters first A-Z, and then special characters last.
How would I go about creating that type of sort? Something in the ORDER BY clause?

Based on a google-cached link to this page:
http://www.google.com/url?sa=t&source=web&cd=3&ved=0CCUQFjAC&url=http%3A%2F%2Fblog.feedmarker.com%2F2006%2F02%2F01%2Fhow-to-do-natural-alpha-numeric-sort-in-mysql%2F&ei=Zg2_TZyKDaffiALjjqwo&usg=AFQjCNGS-rX7AmfrumXK8J7bVSj96bSSmQ
EDIT: Original link is dead.
Here is another link which actually explains what is happening better than the first link did:
http://matthewturland.com/2008/11/05/natural-ordering-in-mysql/
You might try this
SELECT names FROM your_table ORDER BY names + 0 ASC

Select ...
From ...
Order By Case When Col Like '[0-9]%' Then 1 Else 0 End Asc
, Col
Another solution that would account for special characters:
Select ...
From ...
Order By Case When Col Like '[A-Z]%' Then 0 Else 1 End Asc
, Col

This works for me:
SELECT * FROM `yourTable` ORDER BY `yourDatabase`.`yourColumn` ASC

Related

ORDER BY doesn't sort alphabetically

I use the following MySQL query to get results, sorted alphabetically by name:
SELECT * FROM `client` WHERE CONCAT(name, ', ', firstname) LIKE "%ti%" ORDER BY name
In some way the results get sorted, so the ORDER BY works. Unfortunately it works not as expected. The above query gives the following order for example:
Kostic (31)
Hatscher (30)
Why is Kostic listed first? Prove me wrong, but K comes after H in the alphabet... (If it helps - the name field is type of varchar.)
It shouldn't be possible for K to come before H with ORDER BY name. Maybe a blank or even some unprintable character in front?
Test:
Do you get 'Kostic (31)' when you add AND name like 'K%'?
Do you get 'Hatscher (30)' when you add AND name like 'H%' instead?
I know ASC is the default when not specified in the ORDER BY but have you tried putting ASC after.. ORDER BY Name ASC. It's worth a shot

Order by last 3 chars

I have a table like:
id name
--------
1 clark_009
2 clark_012
3 johny_002
4 johny_010
I need to get results in this order:
johny_002
clark_009
johny_010
clark_012
Do not ask me what I already tried, I have no idea how to do this.
This will do it, very simply selecting the right-most 3 characters and ordering by that value ascending.
SELECT *
FROM table_name
ORDER BY RIGHT(name, 3) ASC;
It should be added that as your data grows, this will become an inefficient solution. Eventually, you'll probably want to store the numeric appendix in a separate, indexed integer column, so that sorting will be optimally efficient.
you should try this.
SELECT * FROM Table order by SUBSTRING(name, -3);
good luck!
You may apply substring_index function to parse these values -
select * from table order by substring_index(name, '_', -1)
You can use MySQL SUBSTRING() function to sort by substring
Syntax : SUBSTRING(string,position,length)
Example : Sort by last 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3);
#OR
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, -3,3);
Example : Sort by first 3 characters of a String
SELECT * FROM TableName ORDER BY SUBSTRING(FieldName, 1,3);
Note : Positive Position/Index start from Left to Right and Negative Position/Index start from Right to Left of the String.
Here is the details about SUBSTRING() function.
If you want to order by the last three characters (from left to right) with variable name lengths, I propose this:
SELECT *
FROM TABLE
ORDER BY SUBSTRING (name, LEN(name)-2, 3)
The index starts at lenght of name -2 which is the third last character.
I'm a little late but just encountered the same problem and this helped me.

How to order by a part of a column

i have field with content like this: 1, 2, 100 first two numbers can be any size third one can be up to 100.
Now i need to sort my fields by this third number but since i have 2 other numbers i don't know how to do it.
Maybe i could use something like REGEXP or something else?
So far i've tried SUBSTRING but since my two numbers can be any lenght something like
order by SUBSTRING(my_field, 4)
would work but if i have numbers like 32, 451, 30 it takes wrong numbers
Also i use php for to build my query, but i don't think it matters.
You can use SUBSTRING_INDEX. So something like:
SUBSTRING_INDEX(my_field, ',', -1)
EDIT: if you have spaces you might want to do some trimming as well.
ORDER BY SUBSTRING_INDEX(my_field,',',-1)+0 ASC -- or DESC
Use SUBSTRING_INDEX, which grabs the substring up to the nth occurence of the delimiter (in your case, comma), or, in the case of a negative n, it will return the substring after the nth occurence from the end.
To see what I mean try:
SELECT SUBSTRING_INDEX(my_field,',',-1)
FROM my_table
The reason there is a +0 in the ORDER BY is so that mysql sorts these as numbers, not strings.
This should work:
order by CONVERT(SUBSTRING(my_field, LOCATE(",", my_field, RIGHT)), INTEGER)
SELECT *
FROM (
SELECT
SUBSTRING(my_field, 4) AS my_field,
...
FROM
...
WHERE
...
) temp_table
ORDER BY my_field

MySQL query with alphanumeric order

I'm trying to get a list of some products' model & all models are alphanumeric.
I tried
ORDER BY CAST(field_name AS UNSIGNED)
and
ORDER BY field_name + 0
and
ORDER BY LENGTH(field_name)
and some other ways.
They works for most of them but there are some values that doesn't match the order.
The result I get is like
EAG-75
EAG-110
...
ESCG-500
ESCG-600
...
EYG-40
EYG-55
...
EMG-440
EMG-20
EMG-27
...
EAG-100
...
I don't understand what is causing this.
Please help.
Thanks in advance
You need to sort the 2 bits separately if you want a correct numeric sort per non-numeric prefix
ORDER BY
-- sort by prefix only
LEFT(MyCol, INSTR(col, '-')-1),
-- sort numerically within prefix
CAST(SUBSTRING(MyCol, INSTR(col, '-')+1) AS UNSIGNED)
Would ORDER BY LENGTH(field_name), field_name work better for you?

Specific ordering SQL Command

Say I have a table similar to:
ID Name
1 Test
2 Contest
3 Fittest
4 Testament
Is there a MySQL query I could use with ordering to allow it to display a specific word first?
For example, users are searching for the word "Test". I have a statement similar to "SELECT * FROM table WHERE NAME LIKE '%Test%'". Could I display results to show things that START with Test begin first followed by everything else, while everything is still in alphabetical order.
So output would be:
Test
Testament
Contested
Fittest
Thanks.
This will put your words that begin with Test at the top, and sort those words plus the remainder of the list in alphabetical order..
SELECT * FROM mytable
ORDER BY CASE WHEN name LIKE 'test%' THEN 0 ELSE 1 END ASC, name ASC
SELECT * FROM table
WHERE NAME LIKE '%Test%'
order by case when name like 'test%' then 0 else 1 end