美文网首页
mysql进阶3:排序查询

mysql进阶3:排序查询

作者: 弦好想断 | 来源:发表于2020-04-19 19:20 被阅读0次

    /*
    引入:select * from employees
    语法:select 查询列表 from 表 【where 筛选条件】order by 排序列表 【asc|desc】
    特点:默认升序asc order by后面可以接别名、单个或多个字段、表达式、函数
    order by 一般放在查询语句的最后面,limit子句除外
    order by(本节中执行顺序在第四位(最后))
    */

    案例1:查询员工信息,要求工资从高到低排序

    use myemployees;
    show tables;
    select * from employees order by salary desc;
    select * from employees order by salary asc;
    select * from employees order by salary;
    

    案例2:查询部门编号大于等于90的员工信息,按入职时间的先后进行排序

    select * from employees where department_id>=90 order by hiredate asc;
    

    案例3:按年薪的从高到低显示员工的信息和年薪【按表达式排序】

    select *,salary*12*(1+ifnull(commission_pct,0)) as 年薪 from employees order by 
    salary*12*(1+ifnull(commission_pct,0)) desc;
    

    案例4:按姓名的长度显示员工的姓名和工资【按函数、别名排序】

    select length(concat(last_name,first_name)) as 姓名长度,concat(last_name,first_name) 
    as 姓名,salary from employees order by 姓名长度 desc;
    

    案例5:查询员工信息,先按工资升序,再按员工编号降序[按多个字段排序]

    select * from employees order by salary asc,employee_id desc;
    

    测试

    1.查询员工的姓名和部门编号和年薪,按年薪降序 按姓名升序

    select last_name,department_id,salary*12*(1+ifnull(commission_pct,0)) as 年薪 from 
    employees order by 年薪 desc,last_name asc;
    

    2.选择工资不在8000到17000的员工的姓名和工资,按工资降序

    select last_name,salary from employees where salary not between 8000 and 17000 order by 
    salary desc;
    

    3查询邮箱中包含e的员工信息,并先按邮箱的字节数降序,再按部门号升序

    department_id asc;
    #
    

    相关文章

      网友评论

          本文标题:mysql进阶3:排序查询

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