0

Here is my test data (MySQL)

test1
test2
test3
test10
test11
mytest

I want to Order these record by text, and than by following number (if present):

mytest
test1
test2
test3
test10
test11

And When I use Order By, my data is sorted as text, like so:

mytest
test1
test10
test11
test2
test3

Could anyone provide me a solution?

Beri
  • 11,470
  • 4
  • 35
  • 57
Hassan Raza
  • 671
  • 10
  • 27
  • Do you always have number at the end? Have you seen this question http://stackoverflow.com/questions/12097368/mysql-order-by-string-with-numbers ? Here is also good explanation of the way of doing it: http://www.copterlabs.com/blog/natural-sorting-in-mysql/ – Stepashka Apr 21 '15 at 09:31
  • Dear Stepashka, Actually not sure that some time i have alphabetic record and some time both alphanumeric+alphabetic so in that case what i will do. Like the bellow. asdasda Faisal Town Gulshan ravi new sector Sector Hills sector1 sector2 sector3 sector4 sector10 sector9 Thanks – Hassan Raza Apr 21 '15 at 09:43
  • So the string `sector` is static ? – Abhik Chakraborty Apr 21 '15 at 09:49
  • Clearified question. – Beri Apr 21 '15 at 10:41

1 Answers1

0

In my query I am dividing alphanumeric values from string values, then combing the result,take a look :

  select temp1.a col from
  (select a from tabl WHERE a not REGEXP '[0-9]' order by length(a)) temp1
  union all 
  select temp2.a  from
  (select a FROM tabl WHERE a REGEXP '[0-9]' order by length(a),a) temp2 ;
Sagar Joon
  • 1,387
  • 14
  • 23