举报投诉联系我们 手机版 热门标签 编程学
您的位置:编程学 > orderby子句的功能 ORDER BY 子句

orderby子句的功能 ORDER BY 子句

2023-04-07 15:18 SQL教程

orderby子句的功能 ORDER BY 子句

orderby子句的功能 ORDER BY 子句

orderby子句的功能

SQL ORDER BY 子句

SQL ORDER BY 子句根据一列或者多列的值,按照升序或者降序排列数据。某些数据库默认以升序排列查询结果。

语法

ORDER BY 子句的基本语法如下所示:

    SELECT column-list 
    FROM table_name 
    [WHERE condition] 
    [ORDER BY column1, column2, .. columnN] [ASC | DESC];

ORDER BY 子句可以同时使用多个列作为排序条件。无论用哪一列作为排序条件,都要确保该列在存在。

示例:

考虑含有如下所示记录的 CUSTOMERS 表:

    +----+----------+-----+-----------+----------+
    | ID | NAME     | AGE | ADDRESS   | SALARY   |
    +----+----------+-----+-----------+----------+
    |  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
    |  2 | Khilan   |  25 | Delhi     |  1500.00 |
    |  3 | kaushik  |  23 | Kota      |  2000.00 |
    |  4 | Chaitali |  25 | Mumbai    |  6500.00 |
    |  5 | Hardik   |  27 | Bhopal    |  8500.00 |
    |  6 | Komal    |  22 | MP        |  4500.00 |
    |  7 | Muffy    |  24 | Indore    | 10000.00 |
    +----+----------+-----+-----------+----------+

下面的例子将查询结果按照 NAME 和 SALARY 升序排列:

    SQL> SELECT * FROM CUSTOMERS
         ORDER BY NAME, SALARY;

结果如下所示:

    +----+----------+-----+-----------+----------+
    | ID | NAME     | AGE | ADDRESS   | SALARY   |
    +----+----------+-----+-----------+----------+
    |  4 | Chaitali |  25 | Mumbai    |  6500.00 |
    |  5 | Hardik   |  27 | Bhopal    |  8500.00 |
    |  3 | kaushik  |  23 | Kota      |  2000.00 |
    |  2 | Khilan   |  25 | Delhi     |  1500.00 |
    |  6 | Komal    |  22 | MP        |  4500.00 |
    |  7 | Muffy    |  24 | Indore    | 10000.00 |
    |  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
    +----+----------+-----+-----------+----------+

下面的例子将查询结果按照 NAME 降序排列:

    SQL> SELECT * FROM CUSTOMERS
         ORDER BY NAME DESC;

结果如下所示:

    +----+----------+-----+-----------+----------+
    | ID | NAME     | AGE | ADDRESS   | SALARY   |
    +----+----------+-----+-----------+----------+
    |  1 | Ramesh   |  32 | Ahmedabad |  2000.00 |
    |  7 | Muffy    |  24 | Indore    | 10000.00 |
    |  6 | Komal    |  22 | MP        |  4500.00 |
    |  2 | Khilan   |  25 | Delhi     |  1500.00 |
    |  3 | kaushik  |  23 | Kota      |  2000.00 |
    |  5 | Hardik   |  27 | Bhopal    |  8500.00 |
    |  4 | Chaitali |  25 | Mumbai    |  6500.00 |
    +----+----------+-----+-----------+----------+
阅读全文
以上是编程学为你收集整理的orderby子句的功能 ORDER BY 子句全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 编程学 bianchengxue.com 版权所有 联系我们
桂ICP备19012293号-7 返回底部