The basic idea of using multiple fields in ORDER BY clause is something like the following.
Consider you have a table with the following structure and data
Code:
----------------------------------
empid | empname | salary |
----------------------------------
100 | Johnson | 10000 |
----------------------------------
200 | Adam | 12000 |
----------------------------------
300 | Mike | 11000 |
----------------------------------
400 | Johnson | 17000 |
----------------------------------
500 | Tomyknoker | 10000 |
----------------------------------
You are executing the following SQL statement on the above mentioned table (name of the table is emp).
Code:
SELECT * FROM `emp` ORDER BY empname ASC , salary DESC
The output would be something like the following
Code:
----------------------------------
empid | empname | salary |
----------------------------------
200 | Adam | 12000 |
----------------------------------
400 | Johnson | 17000 |
----------------------------------
100 | Johnson | 10000 |
----------------------------------
300 | Mike | 11000 |
----------------------------------
500 | Tomyknoker | 10000 |
----------------------------------
Now we've performed a first level sorting based on empname and two persons with the same name exist in the above table on thsoe two records only it will perform a second level sorting which is based on empsal field in a descending manner. So in this case the Johnson whose empid is 400 and salary is 17000 came before the other Johnson whose record kept in the original table before the person with an empid 400.
Hope this is clear now.
Bookmarks