0

I'm writing MySQL query for sort this kind of data

Traffic
100/40
12/1
50/20
25/5

the value get by devide right hand side number from left hand side number.

also i have multi columns for sort

ex - SELECT * FROM fltable ORDER BY Traffic DESC,Speed ASC,Cost ASC

I need to sort those data ascending order and descending order, can anyone help me with this.

Thank you

5
  • 4
    What is expected result? Commented Sep 4, 2013 at 11:38
  • Excuse me, for I have to ponder about this paticular data. Commented Sep 4, 2013 at 11:39
  • Are these values stored as VARCHAR? Or you want to order them by the result of the dividing? Commented Sep 4, 2013 at 11:39
  • ascending order or descending order,yes those are in varchar format Commented Sep 4, 2013 at 11:40
  • How do you want to sort them? By the first number before the / or by the second number after the / or by the result of dividing them? Please edit your question and add these details to it. Commented Sep 4, 2013 at 11:42

2 Answers 2

2
SELECT * FROM fractions;
+-----------+
| fractions |
+-----------+
| 100/40    |
| 12/1      |
| 25/5      |
| 50/20     |
+-----------+

SELECT fractions
     , SUBSTRING_INDEX(fractions,'/',1)/SUBSTRING_INDEX(fractions,'/',-1)x 
  FROM fractions 
 ORDER 
    BY x DESC;
+-----------+------+
| fractions | x    |
+-----------+------+
| 12/1      |   12 |
| 25/5      |    5 |
| 100/40    |  2.5 |
| 50/20     |  2.5 |
+-----------+------+
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the answer but i have multi columns for sort ex - SELECT * FROM fltable ORDER BY Traffic DESC,Speed ASC,Cost ASC , do you know how to use this with above query(traffic=fractions), thanks again.
1

Try something like following query:

SELECT *, CONVERT(SUBSTR(var, 1, POSITION('/' IN var) - 1), UNSIGNED INTEGER) as num 
FROM table 
ORDER BY num DESC

Get number before / with SUBSTR and CONVERT to int

Result with num column

100
50
25
12

1 Comment

thanks for the answer but i have multi columns for sort ex - SELECT * FROM fltable ORDER BY Traffic DESC,Speed ASC,Cost ASC , do you know how to use this with above query(traffic=fractions), thanks again.

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.