美文网首页
3.排序检索数据

3.排序检索数据

作者: 升级打怪啊怪 | 来源:发表于2020-06-13 16:52 被阅读0次

    使用select语句的order by子句,对检索出的数据进行排序
    一、排序数据
    对prod_name 列以字母顺序排序数据
    SELECT prod_name FROM products ORDER BY prod_name;

    mysql> SELECT prod_name FROM products ORDER BY prod_name;
    +----------------+
    | prod_name      |
    +----------------+
    | .5 ton anvil   |
    | 1 ton anvil    |
    | 2 ton anvil    |
    | Bird seed      |
    | Carrots        |
    | Detonator      |
    | Fuses          |
    | JetPack 1000   |
    | JetPack 2000   |
    | Oil can        |
    | Safe           |
    | Sling          |
    | TNT (1 stick)  |
    | TNT (5 sticks) |
    +----------------+
    14 rows in set (0.01 sec)
    

    二、按多个列排序
    按多个列排序,只要指定列名,列名之间用逗号分开。
    先按价格,再按名称排序
    SELECT prod_id,prod_price,prod_name FROM products ORDER BY prod_price,prod_name

    mysql> SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price, prod_name;
    +---------+------------+----------------+
    | prod_id | prod_price | prod_name      |
    +---------+------------+----------------+
    | FC      |       2.50 | Carrots        |
    | TNT1    |       2.50 | TNT (1 stick)  |
    | FU1     |       3.42 | Fuses          |
    | SLING   |       4.49 | Sling          |
    | ANV01   |       5.99 | .5 ton anvil   |
    | OL1     |       8.99 | Oil can        |
    | ANV02   |       9.99 | 1 ton anvil    |
    | FB      |      10.00 | Bird seed      |
    | TNT2    |      10.00 | TNT (5 sticks) |
    | DTNTR   |      13.00 | Detonator      |
    | ANV03   |      14.99 | 2 ton anvil    |
    | JP1000  |      35.00 | JetPack 1000   |
    | SAFE    |      50.00 | Safe           |
    | JP2000  |      55.00 | JetPack 2000   |
    +---------+------------+----------------+
    14 rows in set (0.00 sec)
    

    四、指定排序方向
    默认的排序顺序是升序排序(从A到Z),关键字是ASC;降序排序(从Z到A),关键字是DESC

    mysql> SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC;
    +---------+------------+----------------+
    | prod_id | prod_price | prod_name      |
    +---------+------------+----------------+
    | JP2000  |      55.00 | JetPack 2000   |
    | SAFE    |      50.00 | Safe           |
    | JP1000  |      35.00 | JetPack 1000   |
    | ANV03   |      14.99 | 2 ton anvil    |
    | DTNTR   |      13.00 | Detonator      |
    | FB      |      10.00 | Bird seed      |
    | TNT2    |      10.00 | TNT (5 sticks) |
    | ANV02   |       9.99 | 1 ton anvil    |
    | OL1     |       8.99 | Oil can        |
    | ANV01   |       5.99 | .5 ton anvil   |
    | SLING   |       4.49 | Sling          |
    | FU1     |       3.42 | Fuses          |
    | FC      |       2.50 | Carrots        |
    | TNT1    |       2.50 | TNT (1 stick)  |
    +---------+------------+----------------+
    14 rows in set (0.00 sec)
    

    DESC关键字只应用到直接位于其前面的列名
    对多个列排序,价格降序,名称升序

    mysql> SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC, prod_name;
    +---------+------------+----------------+
    | prod_id | prod_price | prod_name      |
    +---------+------------+----------------+
    | JP2000  |      55.00 | JetPack 2000   |
    | SAFE    |      50.00 | Safe           |
    | JP1000  |      35.00 | JetPack 1000   |
    | ANV03   |      14.99 | 2 ton anvil    |
    | DTNTR   |      13.00 | Detonator      |
    | FB      |      10.00 | Bird seed      |
    | TNT2    |      10.00 | TNT (5 sticks) |
    | ANV02   |       9.99 | 1 ton anvil    |
    | OL1     |       8.99 | Oil can        |
    | ANV01   |       5.99 | .5 ton anvil   |
    | SLING   |       4.49 | Sling          |
    | FU1     |       3.42 | Fuses          |
    | FC      |       2.50 | Carrots        |
    | TNT1    |       2.50 | TNT (1 stick)  |
    +---------+------------+----------------+
    14 rows in set (0.00 sec)
    

    五、找出一个列中最高或最低的值
    SELECT prod_price FROM products ORDER BY prod_price DESC LIMIT 1;

    mysql> SELECT prod_price FROM products ORDER BY prod_price DESC LIMIT 1;
    +------------+
    | prod_price |
    +------------+
    |      55.00 |
    +------------+
    1 row in set (0.00 sec)
    

    在MySQL中,A视为和a相同,顺序相同

    子句:SQL语句由子句构成,有些子句是必需的,而有的是可选的。一个子句通常由一个关键字和所提供的数据组成。

    相关文章

      网友评论

          本文标题:3.排序检索数据

          本文链接:https://www.haomeiwen.com/subject/hofztktx.html