Keyword of the order by command of MySQL in PHP

Source: Internet
Author: User
Tags sql tutorial

Keyword of the order by command of MySQL in PHP

Is used to sort data records.


Keyword of this command
Is used to sort data records.

By default, records sorted by command keywords are sorted in ascending order.

If you want to sort records in a descending order, you can use the description of the keyword.

Syntax

SELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESC

For more information about SQL, see our SQL tutorial.

For example
In the following example, all data is stored in the "person" table, and various "Age" columns are displayed:

 

$ 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 Persons order by age "); while ($ row = mysql_fetch_array ($ result) {echo $ row ['firstname']; echo "". $ row ['lastname']; echo "". $ row ['age']; echo "<br/> ";}

mysql_close($con);
 
Output.
Glenn Quagmire 33Peter Griffin 35
 

Command Columns
It is also possible to exceed one column. When you order more than one column, the second column only uses value, if the first column is equal:

 

SELECT column_name(s)FROM table_nameORDER BY column1, column2
For more information, see www.111cn.cn/database/database.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.