Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL 'order by' first, and then next [duplicate]

I have a table, something like

STUDENTNAME   ISMANUALLYADDED   ISEDITED
-----------   ---------------   --------
APPLE              0               0
ANT                0               1
BELL               0               0
DOLL               1               0

Here, I am trying to sort the data by studentname first, by ismanuallyadded, and then by isedited. I am expecting the result like:

StudentName
-----------
APPLE
BELL
DOLL
ANT

For this, I am trying like,

select studentname from table1
order by studentname, ismanuallyadded, isedited

but, this query gives the result like,

Student
-------
APPLE
ANT
BELL
DOLL

Is it possible to order by studentname first and then order by ismanuallyadded?

I am trying to display all the studentnames (neither manually added nor isedited) alphabetically, then only ismanuallyadded students should come, and then isedited.

like image 695
shanish Avatar asked Sep 23 '13 13:09

shanish


2 Answers

The second criteria in the order by is only used when different rows have the same value in the previous criteria.

Your rows all have different studentname values, so the second and third criteria are never used.

Try this:

select studentname from table1
order by isedited, ismanuallyadded, studentname
like image 128
Joorge Leal Avatar answered Sep 20 '22 15:09

Joorge Leal


Try this

select studentname from table1
order by studentname ASC, ismanuallyadded ASC, isedited ASC 
like image 33
Ramesh Rajendran Avatar answered Sep 18 '22 15:09

Ramesh Rajendran