Naren Verma
Naren Verma

Reputation: 2327

Multiple ascending order is not working in Mysql

I have one table called as request and there are three columns called as Id, want, teach. I have to display want and teach column in ascending order and I don't want to display NULL and blank rows.

I tried query only want column is displaying in ascending order but teach column is not displaying in ascending.

  SELECT want, teach FROM request WHERE want IS NOT NULL OR want !='' AND teach IS NOT NULL OR teach !='' GROUP BY want, teach ORDER BY want ASC, teach ASC

If I write ORDER BY teach ASC, want ASC then teach column is displaying in Ascending order and want is showing DESC order. Would you help me in this?

Chech left side column is want and right side column is teach. I am getting output like this. left side column is ASC order but right side is showing DESC enter image description here

Upvotes: 0

Views: 2049

Answers (2)

hassan
hassan

Reputation: 8308

You are not getting the point of multi order here.

firstly , by default, most of dbms sorting as ASC.

Multiple ordering depending on the order of sorted columns, which is mean that -so to speak- :-

" hey sql ! select some result from my table , order them by col1 and group them by this order , then this sorted or grouped data set , sort them by col2. "

it's kind of multidimensional sorting so to speak.

take this as an example :

+------+---------+-------+                   +------+---------+-------+
|  id  |   want  | teach |                   |  id  |   want  | teach |
+------+---------+-------+                   +------+---------+-------+
|   1  |    1    |   2   |    ORDER BY       |   2  |    1    |   1   |
|   2  |    1    |   1   |       want asc,   |   1  |    1    |   2   |
|   3  |    2    |   1   |       teach asc   |   4  |    1    |   2   |
|   4  |    1    |   2   |                   |   3  |    2    |   1   |
+------+---------+-------+                   +------+---------+-------+

and when you revert the order of sorted columns like this :

+------+---------+-------+                   +------+---------+-------+
|  id  |   want  | teach |                   |  id  |   want  | teach |
+------+---------+-------+                   +------+---------+-------+
|   1  |    1    |   2   |    ORDER BY       |   2  |    1    |   1   |
|   2  |    1    |   1   |       teach asc,  |   3  |    2    |   1   |
|   3  |    2    |   1   |       want asc    |   1  |    1    |   2   |
|   4  |    1    |   2   |                   |   4  |    1    |   2   |
+------+---------+-------+                   +------+---------+-------+

Upvotes: 0

krishn Patel
krishn Patel

Reputation: 2599

first you need to understand order by

if you give 1st,2nd asc order it will frist asc the 1st column and depend on this it will asc 2nd column. for example

1st  2nd               
1    b
2    d
3    a
2    a
1    a

order by convert in

 1st   2nd 
  1     a
  1     b
  2     a
  2     d
  3     a

So you need to different query

     SELECT want FROM request WHERE want IS NOT NULL OR want !='' GROUP BY 
 want ORDER BY want ASC

     SELECT teach FROM request WHERE teach IS NOT NULL OR teach !='' GROUP
 BY  teach ORDER BY teach ASC

Upvotes: 1

Related Questions