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?
Related
I want to order a column by its values, lowest to highest. However the column has a prefix set, so that its contents can look like this SR1001000. I've figured that ifi want to order this, i would need to remove the prefix. So for now my Query looks like this:
SELECT a2t_import.*,SUBSTRING(a2t_import.a2t_vare_nr, 3) as partial_vare_nr
FROM a2t_import
ORDER BY partial_vare_nr ASC`
However i also need to only get the rows where the column has a specific prefix which i get by adding a regular expression like so
SELECT a2t_import.*,SUBSTRING(a2t_import.a2t_vare_nr, 3) as partial_vare_nr
FROM a2t_import
WHERE a2t_vare_nr REGEXP '^(SR)+[0-9]+'
ORDER BY partial_vare_nr ASC
This gives me the correct output where the above example looks like this 1001000, but the sorting is not what I'd expect.
I get the following output
10002000
1001000
...
As you can see, the first row is clearly of a higher number than the second. Why is this?
The reason the sorting is off that currently MySQL is treating your computed column as text, not as numerical data. This has the following unwanted side effect:
10002000
1001000
^
The value 10002000 appears first, because it would appear before 1001000 in a dictionary. One trick to workaround this would be to also use the lengths of the strings when sorting. Consider the following comparison:
1000200
1001000
Now 1000200 comes before 1001000, and the lexicographic sort agrees with the numeric sort.
Try the following query:
SELECT a2t_import.*,
SUBSTRING(a2t_import.a2t_vare_nr, 3) AS partial_vare_nr
FROM a2t_import
WHERE a2t_vare_nr REGEXP '^(SR)+[0-9]+'
ORDER BY CHAR_LENGTH(partial_vare_nr), -- shortest strings first
partial_vare_nr -- lexigraphical sort among strings
-- of same length - agrees with numeric sort
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
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.
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
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 &).
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