0

I already have the argument for the st column..

mysql> SELECT * FROM t ORDER BY LEFT(st,LOCATE(' ',st)), CAST(SUBSTRING(st,LOCATE(' ',st)+1) AS SIGNED);

+----+------+
| id | st   |
+----+------+
|  1 | a 1  |
|  3 | a 6  |
|  4 | a 11 |
|  2 | a 11 |
|  5 | b 1  |
|  7 | b 6  |
|  8 | b 12 |
|  6 | b 12 |
+----+------+

this is what it should happen..

+----+------+
| id | st   |
+----+------+
|  1 | a 1  |
|  3 | a 6  |
|  2 | a 11 |
|  4 | a 11 |
|  5 | b 1  |
|  7 | b 6  |
|  6 | b 12 |
|  8 | b 12 |
+----+------+

can I do this or not via SQL?

1 Answer 1

1

Well from what you show the only difference is that "ties" are broken by the ID column. If that's the case you can just use that as the third sort field:

SELECT * 
FROM t 
ORDER BY LEFT(st,LOCATE(' ',st)),  
         CAST(SUBSTRING(st,LOCATE(' ',st)+1) AS SIGNED),
         ID
Sign up to request clarification or add additional context in comments.

3 Comments

This should get you the result you're looking for, but you probably want to split that column up into two others. One for the letter and another for the number. Then you can index the columns and have much faster performance.
@Jody i forgot to ask about that.. rather than splitting them the st data should be without the space. Ex: A1, A6, A11, A11 can sql still process that?
It would be a string so sorting would not work as expected when you have, for example, A1, A2, A11. The comparison would be lexicographic instead of numeric, so the result would be A1, A11, A2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.