SQL ORDER BY Descending and ascending Command
The results we get from a table we may have to display in an order. The
result may be from highest to lowest or lowest to highest in a numeric
field or from A to Z or Z to A in a text or varchar field. We may require
a combination of this also. We can use ORDER BY sql command to manage the
display of records. We will apply this sql command to our student table in MySQL database to
control or manage our order of display.
Highest to lowest order
We can apply this to our numeric field mark to display the list in order of
lowest mark to highest mark by using the ASC command ( ascending command )
. Please note that by default all order by commands are in ascending order only.
Here is the command to display the records in decending order ( from highest to
lowest ) based on the mark field.
SELECT * FROM `student` ORDER BY mark desc
Here the total mark will be displayed in the order of highest to lowest and
class will not be considered. To display the records in order of mark in a
class, we have to use to field names in the order by clause. Here is the code to
display the records in the order of class and then in the order of marks. This
will give a highly use full way by displaying all the records in order of class
and within the class in order of marks.
SELECT * FROM `student` ORDER BY class desc, mark desc
Read how Order by command is used to display highest 3 records of student table
order by on a varchar field
Here is the command to display the records in the order of class
SELECT * FROM `student` ORDER BY class
|id ||name ||class ||mark |
| 4||Krish Star||Four||60|
|6||Alex John|| Four||55|
Here the records are returned in the order by class from A to Z . We can
reverse the order by specifying in our sql command to change the order to
Descending that is from Z to A. We have to add desc to the ORDER BY clause. Here
is the command to do that
SELECT * FROM `student` ORDER BY class desc
How to give the option to the user to change the record display order ?
Number of User Comments : 1
|More on getting records from table with different combinations of commands|
|just want to asked guys, hope you help me this is the scenario i have at least 3 data in my database from ID 1, 2, 3, and i want to display this file or post, data display is OK but i want to display like this 3, 2, 1 how can make it that way.|