MySQLOrder By keyword is used to classify the data in the record.
MySQL Order By Keyword Classification
Order by keyword is used to classify the data in the record.
MySQL Order By syntax
SELECT column_name (s)
FROM table_name
Order by column_name
Note: The SQL statement is a "case-insensitive" Statement (which does not distinguish between uppercase and lowercase letters), that is, "ORDER BY" and "order by" are the same.
MySQL Order By case
The following example shows how to select all records from the "Person" table and classify the "Age" column:
<? Php
$ Con = mysql_connect ("localhost", "peter", "abc123 ");
If (! $ Con)
{
Die ('could not connect: '. mysql_error ());
}
Mysql_select_db ("my_db", $ con );
$ Result = mysql_query ("SELECT * FROM person order by age ");
While ($ row = mysql_fetch_array ($ result ))
{
Echo $ row ['firstname']
Echo "". $ row ['lastname'];
Echo "". $ row ['age'];
Echo "<br/> ";
}
Mysql_close ($ con );
?>
The above code will output the following results:
Glenn Quagmire 33
Peter Griffin 35
Sort by ascending or descending order
If you use the "order by" keyword, all records are sorted in ascending order by default (from 1 to 9, from a to z)
Use the "DESC" keyword to sort all data in descending order (from 9 to 1, from z to ):
SELECT column_name (s)
FROM table_name
Order by column_name DESC
MySQL Order By is classified By two columns
Most of the time, we need to classify data based on the two columns (or more columns) at the same time. When the specified number of columns is greater than one, the second column is referenced only when the values of the first column are identical:
SELECT column_name (s)
FROM table_name
Order by column_name1, column_name2
References:
MySQL Order By index optimization: http://www.phpq.net/mysql/mysql-order-by.html
MySQL Order By syntax: http://www.phpq.net/mysql/mysql-order-by-syntax.html
MySQL Order By Rand () Efficiency: http://www.phpq.net/mysql/mysql-order-by-rand.html
MySQL Order By usage: http://www.phpq.net/mysql/mysql-order-by-use.html