0

Here's the case, I use a MySQL query like this

SELECT 
    *
FROM
    tbl_name
WHERE
    id = 1
ORDER BY LENGTH(title), title;

but when there are titles like

Detective Conan Episode 797.5 English Subbed

Detective Conan Episode 829.2 English Subbed

It becomes

Detective Conan Episode 897 English Subbed

Detective Conan Episode 898 English Subbed

Detective Conan Episode 797.5 English Subbed

Detective Conan Episode 829.2 English Subbed

The correct one must be like this

Detective Conan Episode 797.5 English Subbed

Detective Conan Episode 829.2 English Subbed

Detective Conan Episode 897 English Subbed

Detective Conan Episode 898 English Subbed

Is it possible in a MySQL query to sort like what I want? or should I redesign the table structure and split the episode number in a new column.

Thank you.

Community
  • 1
  • 1

2 Answers2

0

I misunderstood your initial request. Try this :

SELECT 
    *
FROM
    tbl_name
WHERE
ORDER BY CAST(title AS UNSIGNED), title;

Check fiddle example here : http://sqlfiddle.com/#!9/755b97/2

Edit : This seems to work only when numbers are at the front.

This seems to be the only working solution for your problem, but at the cost of heavy load : Natural Sort in MySQL

Other solution would be for you to switch to mysql 8.0, which introduces regexp_replace, allowing you to extract the numbers from your string, then ORDER BY CAST(the_result_of_your_regexp). Would still be heavy though.

Aurelien
  • 1,497
  • 7
  • 15
0

@Aurelien its working become descending if i use:

SELECT 
    *
FROM
    tbl_name
WHERE
    id = 1
ORDER BY LENGTH(title) DESC, title DESC

but there's still something wrong if the title like in the picture

Check this