1

I have select query for the ascending order like...

select DISTINCT
    (m.task_name) as TaskName, 
    m.activity_name as ActivityName,
    m.actual_score,
    m.score,
    m.section_name 
from 
    users u, 
    som_scores m 
where 
    m.ref_student_id = '".$row['userid']."' and 
    m.section_name IN('jws1','jws2','jws3','jws4','sm1','srs1','Pws1','Pws2','lws1','sp1') 
ORDER BY 
    m.section_name ASC

I want to set the Section name in ascending order and also make the task name order by like Task1 Task2 and so on..

Means I want to display data like

1) sections name = jws1 and under the section like task Task1 Task2 Task3 Task4

2) sections name = jws2 and under the section like task Task1 Task2 Task3 Task4

And so on..

Glavić
  • 42,781
  • 13
  • 77
  • 107
Nishant Patel
  • 335
  • 1
  • 5
  • 23

3 Answers3

2

Just add secondary order in ORDER BY

ORDER BY 
    section_name ASC,
    TaskName ASC
Glavić
  • 42,781
  • 13
  • 77
  • 107
  • but using this code i get the answer like Task 1 Task 11 Task 12 Task 3 Task 4 This sequence i get the data – Nishant Patel Oct 06 '12 at 11:40
  • 1
    It would be best if you had a numeric field with the task number, instead of a "Task #" string. But see [Natural Sort in MySQL](http://stackoverflow.com/questions/153633/natural-sort-in-mysql) for a solution. – Barmar Oct 06 '12 at 12:04
0

use this query

select DISTINCT(m.task_name) as TaskName, m.activity_name as ActivityName,m.actual_score,m.score,m.section_name from users u, som_scores m where m.ref_student_id = '".$row['userid']."' and m.section_name IN('jws1','jws2','jws3','jws4','sm1','srs1','Pws1','Pws2','lws1','sp1') ORDER BY m.section_name ASC,m.task_name ASC

This will order by section first and after that by task name

try this order

m.task_name+0 ASC

OR

ORDER BY m.section_name ASC,CAST(SUBSTRING(m.task_name,LOCATE(' ',m.task_name)+1) AS SIGNED) ASC
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

I found solution like this way "cast( SUBSTRING(TaskName, 5 ) AS unsigned ) ASC"

select DISTINCT
        (m.task_name) as TaskName, 
        m.activity_name as ActivityName,
        m.actual_score,
        m.score,
        m.section_name 
    from 
        users u, 
        som_scores m 
    where 
        m.ref_student_id = '".$row['userid']."' and 
        m.section_name IN('jws1','jws2','jws3','jws4','sm1','srs1','Pws1','Pws2','lws1','sp1') 
    ORDER BY 
        m.section_name ASC,
        cast( SUBSTRING(TaskName, 5 ) AS unsigned ) ASC,
        ActivityName ASC
Vikas S Singh
  • 1,748
  • 1
  • 14
  • 29
Nishant Patel
  • 335
  • 1
  • 5
  • 23