分组查询 group by

  • 将某个字段的相同值分为一组,对其他字段的数据进行聚合函数的统计,称为分组查询

  • 单字段分组查询

    • 1.查询每个部门的平均工资

      select dept_id,avg(sal) from emp group by dept_id;

    • 2.查询每个职位的最高工资

      select job, max(sal) from emp group by job;

    • 3.查询每个部门的人数

      select dept_id,count(*) from emp group by dept_id;

    • 4.查询每个职位 中工资大于1000的人数

      select job,count(*) from emp where sal>1000 group by job ;

    • 5.查询每个领导手下的人数

      select mgr,count(*) from emp where mgr is not null and mgr <>0 group by mgr;

    • 练习

      select job,avg(sal) from emp where avg(sal)>2000 group by job;
      
      select job,avg(sal)  from emp group by job having avg(sal) >2000;
      
      select job, count(*) c from emp group by job having c > 1;
      
      select dept_id,sum(sal) s from where mgr is not null emp group by dept_id having s>5000;
      
      select dept_id,avg(sal) where sal between 1000 and 3000 group by dept_id having>=2000;
      
      select * from emp where sal>(select avg(sal) from emp where dept_id=2);
  • 多字段分组查询

    • 1.查询每个部门每个主管手下的人数;

      select job,,count(*) where mgr is null group by dept_id,

  • having

    • 后跟聚合函数的条件;

    • 不能单独使用,常和group by组队出现,跟在其后面

      select job count(*) c from emp group by job having c>1000;

字段查询顺序

  • select 查询字段信息 from 表名 where 普通字段条件 group by

    分组字段名 having 聚合函数条件 order by 排序字段名 limit 跳过条数,请求条数;

子查询(嵌套查询)

  1. 查询工资大于2号部门平均工资的员工信息

  2. 查询员工高于程序员最高工资的员工信息

    select  * from emp where sal>(select  max(sal) from emp where job='程序员');
  3. 查询工资最高的员工信息

    select  name,max(sal) from emp group by name;
  4. 查询和孙悟空相同工作的员工信息

    select job from emp where name='孙悟空';
    
    select  * from emp where job=(select job from emp where name='孙悟空') and name<>'孙悟空';
  5. 查询那最低工资员工的同事们的信息,(同事指同一部门)

    select min(sal) from emp ;
    
    select  dept_id from emp where sal=(select min(sal) from emp)
    
    select * from where dept_id=(select dept_id from sal=(select min(sal) from emp));

关联查询之等值连接

  • 格式: select * from A,B where 关联关系

  • 1.查询每个员工 姓名和对应的部门名字

    select e.name,d.name from emp e,dept d where e.dept_id=d.id and sal  > 2000;

关联查询之内连接

  • 格式: select * from A join B on 关联关系

  • 1.查询工资高于2000的员工姓名和对应的部门名

     select e.name,d.name from emp e join dept d on e.dept_id =d.id where sal>2000;
    
     select e.name ,d.name from emp e join dept d on  e.dept_id=d.id where sal >2000;
    
    
    1. 查询有领导并且和销售有关的员工姓名,工作,部门名,部门地点

关联查询之外连接

  • 等值连接和内连接查询到的都是两张表的交集数据;

  • 外连接查询的是一张表 全部和另一张表的交集数据

  • 格式 select * from A left join B on 关联关系

    1.查询所有员工姓名和对应的部门名
    
    select e.name,d.name  from emp e left join dept d on e.dept_id=d.id;
    
    select e.name,d.name from emp  emp e left join dept d on e.dept_id=id;
    
    select e.name,d.name from emp e  left  join  dept d on e.dept_id=d.id;
    
    2.查询所有部门的名称/地点和对应的员工姓名和工资
    
    select d.name,d.loc,e.name,e.sal from emp e left join dept d on e.dept_id =d.id;

11 MySQL_分组查询和关联查询的更多相关文章

  1. Mongoose如何实现统计查询、关联查询

    [问题]Mongoose如何实现统计查询.关联查询  发布于 4 年前  作者 a272121742  13025 次浏览 最近业务上提出一个需求,要求能做统计,我们设计的文档集,统计可能跨越的文档会 ...

  2. MySQL查询(关联查询)

    一.mysql查询与权限 (一)数据库关联查询 **内连接查询(inner join)** 查询两个表共有的数据,交集 SELECT * FROM tb1 INNER JOIN tb2 ON 条件 所 ...

  3. mysql系列九、mysql语句执行过程及运行原理(分组查询和关联查询原理)

    一.背景介绍 了解一个sql语句的执行过程,了解一部分都做了什么,更有利于对sql进行优化,因为你知道它的每一个连接.where.分组.子查询是怎么运行的,都干了什么,才会知道怎么写是不合理的. 大致 ...

  4. Mybatis高级查询之关联查询

    learn from:http://www.mybatis.org/mybatis-3/zh/sqlmap-xml.html#Result_Maps 关联查询 准备 关联结果查询(一对一) resul ...

  5. day95:flask:SQLAlchemy数据库查询进阶&关联查询

    目录 1.数据库查询-进阶 1.常用的SQLAlchemy查询过滤器 2.常用的SQLAlchemy查询结果的方法 3.filter 4.order_by 5.count 6.limit&of ...

  6. Mysql子查询、关联查询

    mysql中update.delete.install尽量不要使用子查询 一.mysql查询的五种子句         where(条件查询).having(筛选).group by(分组).orde ...

  7. Mysql - 查询之关联查询

    查询这块是重中之重, 关系到系统反应时间. 项目做到后期, 都是要做性能测试和性能优化的, 优化的时候, 数据库这块是一个大头. sql格式: select 列名/* from 表名 where 条件 ...

  8. SQL笛卡尔积查询与关联查询性能对比

    首先声明一下,sql会用略懂,不是专家,以下内容均为工作经验,聊以抒情. 今天帮忙验证同事发布的端口时,查看了一下相关sql内容,发现其使用的sql语句会导致笛卡尔积现象,为了帮其讲解进行了如下分析: ...

  9. 【sql】关联查询+表自关联查询

    表: 经销商 dealer   字段 uid  parent_uid  name 联系人 contact  字段 uid  dealer_id  contact_main 需求: 想要查询到经销商的信 ...

随机推荐

  1. 「BUAA OO Unit 2 HW8」第二单元总结

    「BUAA OO Unit 2 HW8」第二单元总结 目录 「BUAA OO Unit 2 HW8」第二单元总结 Part 0 前言 Part 1 第五次作业 1.1 作业要求 1.2 架构设计 1. ...

  2. Linux强制用户首次登录修改密码

    一个执着于技术的公众号 地方 前言 Linux强制用户首次登陆修改密码,这应该是RHCE认证中用户管理部分, 属于很基础的内容了.可是我忘记了,所以就有了下面的记录~ 实验过程 1.创建用户并设置登录 ...

  3. 微服务生态组件之Spring Cloud OpenFeign详解和源码分析

    Spring Cloud OpenFeign 概述 Spring Cloud OpenFeign 官网地址 https://spring.io/projects/spring-cloud-openfe ...

  4. 前端HTML-01

    HTML是什么? 超文本标记语言,是一种用于创建网页的标记语言 文件的扩展名:.html或者.htm HTML不是什么? HTML是一种标记语言,不是变成语言. HTML文档结构 <!DOCTY ...

  5. 2┃音视频直播系统之浏览器中通过 WebRTC 拍照片加滤镜并保存

    一.拍照原理 好多人小时候应该都学过,在几张空白的纸上画同一个物体,并让物体之间稍有一些变化,然后连续快速地翻动这几张纸,它就形成了一个小动画,音视频播放器就是利用这样的原理来播放音视频文件的 播放器 ...

  6. Spring Boot 动态修改 log level

    引入依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  7. Soa: 一个轻量级的微服务库

    Soa 项目地址:Github:MatoApps/Soa 介绍 一个轻量级的微服务库,基于.Net 6 + Abp框架 可快速地将现有项目改造成为面向服务体系结构,实现模块间松耦合. 感谢 Rabbi ...

  8. Java 15 新特性:隐藏类

    什么是隐藏类 隐藏类,是一种不能被其他类直接使用的类.引入隐藏类的主要目的是给框架来使用,使得框架可以在运行时生成类,并通过反射间接使用它们.可能有点抽象,不要紧,下面我们通过一个例子来直观的认识它! ...

  9. MongoDB 体系结构与数据模型

    每日一句 If no one else guards the world, then I will come forward. 如果没有别人保卫这个世界,那么我将挺身而出. 概述 MongoDB主要是 ...

  10. 双webview模式,子窗口打不开或者无法切换

    iOS 真机调试时,发现window.open 无效.可以结合plusReady里面不执行一起参考,博主在当时遇到这个问题只查询了资料,而后并没有来得及自己亲自验证以下方法的可行性.来日再遇上mui的 ...