您的位置:首页 > 数据库 > MySQL

MySQL查询语句之分页查询和联合查询(七)

2019-04-21 19:59 183 查看
版权声明:版权所有,侵权必究! https://blog.csdn.net/weixin_43216903/article/details/89438175

文章目录

  • 联合查询
  • 分页查询

    应用场景

    当要显示的数据,一页显示不全,需要分页提交sql请求

    语法

    select 查询列表
    from 表
    [join type join 表2
    on 连接条件
    where 筛选条件
    group by 分组字段
    having 分组后的筛选
    order by 排序的字段]
    limit offset,size;

    offset要显示条目的起始索引(起始索引从0开始)
    size要显示的条目个数

    特点

    limit
    语句放在查询语句最后
    ②公式
    要显示的页数page,每页的条目数size

    select 查询列表
    from 表
    limit (page-1)*size,size;

    案例1: 查询前五条员工信息

    use myemployees;
    select * from employees limit 0,5; # 0代表从第一条开始,5代表总条数
    select * from employees limit 5; # 默认从0开始

    案例2: 查询第11条到第25条

    select * from employees limit 10,15;

    案例3: 有奖金的员工信息,并且工资较高的前十名显示出来

    select * from employees
    where commission_pct is not null
    order by salary desc
    limit 10;

    联合查询

    语法:

    查询语句1
    union
    查询语句2
    union
    ...

    应用场景:

    要查询的结果来自多个表,且多个表没有直接的连接关系,但查询的信息一致

    特点:★★★

    1.要求多条查询语句的查询语句得查询列数是一致的
    2.要求多条车厢内语句的查询的每一列的类型和顺序最好一致
    3.

    union
    关键字默认去重,如果使用
    union all
    可以包含重复项

    引入案例: 查询部门编号>90或邮箱包含a的员工信息

    use myemployees;
    select * from employees where email like '%a%' or department_id>90;
    
    select * from employees where email like '%a%'
    union
    select * from employees where department_id>90; # 查询信息一致
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: