The
MySQL ORDER BY keyword is used to categorize the data in a record. The
MySQL ORDER BY keyword is used to classify the data in a record according to the keyword category
, keyword.
MySQL ORDER by Syntax
SELECT column_name (s)
table_name
ORDER BY column_name
Note: SQL statement is " Letter case Insensitive statement (it does not distinguish between uppercase and lowercase letters), that is, "order by" is the same as "an order by".
The following example of a MySQL order by case
: Select All records from the person table and categorize The Age column:
<?php
$con = mysql_connect ("Loc Alhost "," 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 code above will output the following result:
Glenn quagmire
Peter Griffin br> sorted in ascending or descending order
If you use the "ORDER BY" keyword, all records are sorted by default ascending (that is, from 1 to 9, from A to Z)
to makeWith the "DESC" keyword, you can make all the data in descending order (that is, from 9 to 1, from Z to a):
SELECT column_name (s)
from table_name
ordered by column_name DESC
MySQL order is categorized by two columns
many times, we need to classify data based on two columns of content (or more). When the specified number of columns is more than one column, the second column is referenced only when the value in the first column is 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