MySQL ORDER BY syntax

Source: Internet
Author: User
Tags rand

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.