zoukankan      html  css  js  c++  java
  • MYSQL-分组查询-where和having的区别

    -- DQL查询操作
    -- 查询全部字段
    select * from 表名;

    -- 按照条件查询全部字段
    select * from 表名 where 筛选条件

    -- 查询指定字段的数据
    select 列名,列名1 from 表名 where 筛选条件;

    -- 分组查询as起别名,如未分组统计查询全部结果
    select 组列表,count() as 别名 from 表名 where 查询表的筛选条件 group by
    分组的字段 having 分组置灰的结果筛选

    -- 聚合函数
    count(*|字段名)-- *统计数据个数 字段名统计字段下部位null值得数据个数
    -- 求和 sum(字段名)
    select sum(求和字段) from 表名;
    select deptno,SUM(esal) as 部门求和 from emp group by deptno;

    -- AVG求平均值
    select avg(esal) from emp;
    SELECT deptno,avg(esal) as 部门平均 from emp group by deptno;

    -- max查询结果中最大值
    SELECT deptno,max(esal) as 部门最大 from emp group by deptno;

    -- min查询结果最小值
    SELECT deptno,min(esal) as 部门最小 from emp group by deptno;

    -- 在分组的情况下 列出指定字段数据GROUP_CONCAT
    -- 列出指定姓名
    SELECT deptno,GROUP_CONCAT(ename) as 部门最小 from emp group by deptno;

    -- where和having的区别
    -- where后面的筛选条件筛选表中数据
    -- having对分组后进行筛选
    -- 对大于7000的员工分组部门筛选并列出对应员工姓名
    SELECT deptno,GROUP_CONCAT(ename) from emp where esal >7000 group by deptno;

    -- 在以上的情况下 筛选出部门大于1的
    select deptno,GROUP_CONCAT(ename) from emp where esal > 7000 group by deptno;

     

  • 相关阅读:
    图文详解QT布局管理器
    osg中放大缩小模型
    osgearth中XML文档读取报错
    中国河南省洛阳市嵩县黄庄乡红堂村大树芽组
    GIS数据下载整合
    四面体剖分相关开源软件列表
    在你的QT工程中使用ui文件
    对osgAnimation例子的注释的注释
    [debug]调试Release版本应用程序
    链表面试题总结
  • 原文地址:https://www.cnblogs.com/cheng10/p/13590848.html
Copyright © 2011-2022 走看看