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
Here is the command to display the records in the order of class
SELECT * FROM `student` ORDER BY class
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
Order by more than one column of a table
We can display list of students based on their mark and based on their name.
SELECT * FROM student ORDER BY mark , name
This will list on ascending order of mark. When there are more than one student got the same mark ( say 88 ) then the names of them will be listed alphabetically.
So the list will display Bigy above Giff Tow though both of them got equal mark ( say 88 )
If you want to reverse the order of Names you can change the query like this
SELECT * FROM student ORDER BY mark , name DESC
If you want listing should be from highest mark to lowest mark then query should be
SELECT * FROM student ORDER BY mark DESC, name DESC
How to use in PHP Script
You can read more on SQL SELECT query to see how this query can be used in PHP Script to display records. Only the query part is required to be changed.
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.
Suppose if we are using 1st, 2nd and 3rd in class instead of two three and four how will this query will work in that case
The desc qualifier (which stands for descending, i.e. high to low) changes the sequence from the default of low to high.
Data is ordered depending on the data type. Text is ordered according to collating sequence, numbers from low to high (e.g. -100 is before 5), and dates are ordered from earliest to latest.
So 'Three' is greater than 'Four' because T is after F in the collating sequence.
But 3 is less than 4 whether stored as numbers or text.