In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
MySQL sorting
We know that SQL SELECT statements are used to read data from the MySQL table.
If we need to sort the data we read, we can use the ORDER BY clause of MySQL to set which field and which way you want to sort, and then return the search results.
Grammar
The following is the SQL SELECT statement that uses the ORDER BY clause to sort the query data before returning the data:
SELECT field1, field2,...fieldN table_name1, table_name2...ORDER BY field1, [field2...] [ASC [DESC]]
You can use any field as a condition for sorting to return the sorted query results.
You can set multiple fields to sort.
You can use the ASC or DESC keywords to set whether the query results are sorted in ascending or descending order. By default, it is arranged in ascending order.
You can add a WHERE...LIKE clause to set the condition.
Use the ORDER BY clause at the command prompt
The following will use the ORDER BY clause in the SQL SELECT statement to read the data in the MySQL data table runoob_tbl:
Example
Try the following example and the results will be sorted in ascending and descending order.
SQL sorting
Mysql > use RUNOOB;Database changedmysql > SELECT * from runoob_tbl ORDER BY submission_date ASC +-+ | runoob_id | runoob_title | runoob_author | submission_date | +- -+-+ | 3 | Learning Java | RUNOOB.COM | 2015-05-01 | | 4 | Learning Python | RUNOOB.COM | 2016-03-06 | | 1 | Learning PHP | Rookie tutorial | 2017-04-12 | | 2 | Learning MySQL | Rookie tutorial | 2017-04-12 | +-+- -+ 4 rows in set (0.01sec) mysql > SELECT * from runoob_tbl ORDER BY submission_date DESC +-+ | runoob_id | runoob_title | runoob_author | submission_date | +- -+-+ | 1 | study PHP | Rookie tutorial | 2017-04-12 | | 2 | study MySQL | Rookie tutorial | 2017-04-12 | | 4 | study Python | RUNOOB.COM | 2016-03-06 | | 3 | study Java | RUNOOB.COM | 2015-05-01 | +-- -+ 4 rows in set (0.01 sec)
Read all the data in the runoob_tbl table and sort it in ascending order of the submission_date field.
Using the ORDER BY clause in PHP scripts
You can use the mysqli_query () of the PHP function and the same SQL SELECT command with the ORDER BY clause to get the data.
This function is used to execute the SQL command and then output the data of all queries through the PHP function mysqli_fetch_array ().
Example
Try the following example, and the queried data is sorted in descending order of the submission_date field and returned.
MySQL ORDER BY Test:
The output is shown in the following figure:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.