MySQL ORDER BY Syntax introduction _mysql

Source: Internet
Author: User
A bit of a problem occurred today in the process of using the order by, and it was wrong to understand that it was previously understood.

See the use of order by on the W3s Web site before, thinking that the selected data is sorted in ascending or descending order of keywords, and as a result of today's attempt to select DataSet data, it is found that the query result using order BY and by Desc is completely different. According to their previous understanding they should be the same result, and the internal order is not the same.

Asked a colleague, checked the document, before it dawned. If we use ORDER by (DESC) when executing a SELECT statement, it first sorts all records by keyword, then reads the required records sequentially instead of selecting the records and then descending the order. A conceptual error, so write down to warn yourself.

The MySQL order BY keyword is used to classify the data in the record.

MySQL ORDER BY keyword classified by keyword

The ORDER BY keyword is used to classify the data in a record.

MySQL ORDER BY syntax
Copy Code code as follows:

SELECT column_name (s)
From table_name
ORDER BY column_name

NOTE: The SQL statement is a "letter case insensitive" statement (it does not distinguish between uppercase and lowercase letters), that is, "order by" is the same as the "by".

MySQL ORDER BY case

The following example: Select all the records from the person table and categorize the Age column:
Copy Code code as follows:

<?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");
while ($row = Mysql_fetch_array ($result))
{
echo $row [' FirstName ']
echo "". $row [' LastName '];
echo "". $row [' age '];
echo "<br/>";
}
Mysql_close ($con);
?>
[HTML]
The above code will output the following result:

Glenn Quagmire 33

Peter Griffin 35

Sort by ascending or descending order

If you use the "ORDER BY" keyword, all records will be sorted by default ascending (i.e.: from 1 to 9, from A to Z)

Use the "DESC" keyword to make all the data in descending order (that is, from 9 to 1, from Z to a):
[Code]
SELECT column_name (s)
From table_name
ORDER BY column_name DESC

MySQL order BY is sorted 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 of the first column is exactly the same:
Copy Code code as follows:

SELECT column_name (s)
From table_name
Order BY COLUMN_NAME1, column_name2
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.