zoukankan      html  css  js  c++  java
  • A2-02-10.DML-MySQL ORDER BY: Sort a Result Set

    转载自:http://www.mysqltutorial.org/mysql-order-by/

    MySQL ORDER BY: Sort a Result Set

     

    Summary: in this tutorial, you will learn how to sort a result set using MySQL ORDER BY clause.

    Introduction to MySQL ORDER BY clause

    When you use the SELECT statement to query data from a table, the result set is not sorted in any orders. To sort the result set, you use the ORDER BY clause. The ORDER BY  clause allows you to:

    • Sort a result set by a single column or multiple columns.
    • Sort a result set by different columns in ascending or descending order.

    The following illustrates the syntax of the ORDER BY  clause:

    The ASC stands for ascending and the DESC stands for descending. By default, the ORDER BY  clause sorts the result set in ascending order if you don’t specify ASC or DESC explicitly.

    Let’s practice with some examples of using the ORDER BY clause.

    MySQL ORDER BY examples

    See the following customers table in the sample database.

    customers_table

    The following query selects contacts from the customers table and sorts the contacts by last name in ascending order.

    Try It Out

    MySQL ORDER BY example

    If you want to sort the contacts by last name in descending order, you specify the DESC after the contactLastname column in the ORDER BY clause as the following query:

    Try It Out

    MySQL ORDER BY DESC example

    If you want to sort the contacts by the last name in descending order and the first name in ascending order, you specify both  DESC and ASC in the corresponding column as follows:

    Try It Out

    MySQL ORDER BY mutilple columns

    In the query above, the ORDER BY  clause sorts the result set by the last name in descending order first and then sorts the sorted result set by the first name in ascending order to produce the final result set.

    MySQL ORDER BY sort by an expression example

    The ORDER BY clause also allows you to sort the result set based on an expression.

    See the following  orderdetails table.

    order_details_table

    The following query selects the order line items from the orderdetails table. It calculates the subtotal for each line item and sorts the result set based on the order number, order line number, and subtotal.

    Try It Out

    MySQL ORDER expression

    To make the query more readable, you can sort by the column alias as the following query:

    Try It Out

    MySQL ORDER BY alias

    We used subtotal as the column alias for the expression  quantityOrdered * priceEach and sorted the result set based on the subtotal alias.

    MySQL ORDER BY with custom sort order

    The ORDER BY  clause enables you to define your own custom sort order for the values in a column using the FIELD()  function.

    See the following orders table.

    orders table

    For example, if you want to sort the orders based on the following status by the following order:

    • In Process
    • On Hold
    • Cancelled
    • Resolved
    • Disputed
    • Shipped

    You can use the FIELD function to map those values to a list of numeric values and use the numbers for sorting; See the following query:

    Try It Out

    MySQL ORDER BY and FIELD function

    In this tutorial, we’ve shown you various techniques to sort a result set by using the MySQL ORDER BY clause.

  • 相关阅读:
    kibana.yml(中文配置详解)
    Elasticsearch之marvel(集群管理、监控)插件安装之后的浏览详解
    ElasticSearch vs Lucene多维度分析对比
    ElasticSearch 应用场景
    ElasticSearch 在Hadoop生态圈的位置
    ElasticSearch 工作原理
    ElasticSearch 架构图
    ElasticSearch vs 关系型数据库
    Codeforces Round #311 (Div. 2)
    uva 568(数学)
  • 原文地址:https://www.cnblogs.com/zhuntidaoren/p/9511969.html
Copyright © 2011-2022 走看看