MySQLOrderBy syntax introduction bitsCN.com encountered a problem in the use of order by today, and found that it was wrong to understand order.
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 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:
$ 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"
";
}
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:
SELECT column_name (s)
FROM table_name
Order by column_name1, column_name2
BitsCN.com
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.