• MySql(五)select排序查询


    基本语法:

    SELECT 查询列表
    FROM 表名
    WHERE 查询条件
    ORDER BY 排序列表(asc或desc)
    1
    2
    3
    4
    特点:

    order by 子句可以支持单个字段、多个字段、表达式、函数、别名的查询
    order by 一般位置查询语句的最后面(limit子句除外)
    举个栗子
    /**查询员工信息,要求工资按照从高到低进行排序(默认升序)**/
    SELECT * FROM employees ORDER BY salary ASC;
    /**方法2:**/
    SELECT * FROM employees ORDER BY salary;
    /**查询部门标号大于等于90的员工信息,并且按照入职时间进行先后排序**/
    SELECT * FROM employees
    WHERE department_id >= 90
    ORDER BY hiredata ASC;
    /**按照员工的年薪的高低显示员工的信息和年薪**/
    SELECT *, salary*12(1+IFNULL(commission_pct,0) 年薪
    FROM employees
    ORDER BY 年薪 ASC;
    /**按照姓名的长度显示员工的姓名和工资**/
    SELECT LENGTH(last_name) 姓名字节长度 ,last_name,salary
    FROM employees
    ORDER BY 姓名字节长度;
    /**查询员工信息,先按工资升序排序,在按照员工编号降序排序**/
    SELECT * FROM employees ORDER BY salary ASC,employee_id DESC;

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    进阶练习:

    /**选择工资不在8000-9000的员工的姓名和工资,按照工资的降序排序**/
    SELECT last_name,salarty
    FROM employees
    WHERE salary NOT BETWEEN 8000 AND 9000
    ORDER BY salary DESC;
    /**查询邮箱中包含e的员工信息,先按照邮箱的字节数升序排序,在按照部门的降序排序**/
    SELECT *
    FROM employees
    WHERE email LIKE('%e%')
    ORDER BY LENGTH(email) ASC, department_id DESC;
    --------------------- 

  • 相关阅读:
    最大相邻差值 数学 桶排序
    hdu-1536 S-Nim SG函数
    博弈论初步(SG函数)
    hdu-5009 Paint Pearls DP+双向链表 with Map实现去重优化
    hdu-5015 233 Matrix 矩阵快速幂
    hdu-1430 魔板 康拓展开+映射优化
    hdu-1043 bfs+康拓展开hash
    康拓展开-排列的hash
    Daily Scrum 12.23
    Daily Scrum 12.22
  • 原文地址:https://www.cnblogs.com/ly570/p/10987467.html
Copyright © 2020-2023  润新知