MySQL Order By syntax

Source: Internet
Author: User

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

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.