MySQL Order By syntax

Source: Internet
Author: User

A problem occurred while using order by today. I found that the previous understanding of ORDER BY is incorrect.

I saw the order by usage on the w3s website and thought it was to sort the selected data in ascending or descending ORDER of the keywords. When I tried to select the dataset data today, it is found that the query results obtained BY using order by and order by desc are completely different. According to your previous understanding, they should have the same results, but the internal ORDER is different.

I asked my colleagues and checked the document to find out. If we use order by (DESC) when executing the select statement, it first sorts all records BY keywords and then reads the required records in sequence, instead of selecting records first and then sorting them in descending order. A conceptual error, so remember to warn yourself.

MySQL Order 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 syntaxCopy codeThe Code is as follows: 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:Copy codeThe Code is 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 order by age ");
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 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 ):
[Code]
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:Copy codeThe Code is 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.