zoukankan      html  css  js  c++  java
  • MySQL 分组最值、分组均值、分组求和

    数据表

    假设有 student 表,数据如下

    +----+-------+-------+----------+
    | id | name | score | class_id |
    +----+-------+-------+----------+
    | 1 | 刘备1 | 90 | 1 |
    | 2 | 刘备2 | 88 | 2 |
    | 3 | 张飞1 | 70 | 1 |
    | 4 | 张飞2 | 99 | 2 |
    +----+-------+-------+----------+

    问题 1:求每个班的最高分

    解:select class_id, max(score) from student group by class_id;

    +----------+------------+
    | class_id | max(score) |
    +----------+------------+
    | 1 | 90 |
    | 2 | 99 |
    +----------+------------+

    问题 2:求每个班的最低分

    解:select class_id, min(score) from student group by class_id;

    +----------+------------+
    | class_id | min(score) |
    +----------+------------+
    | 1 | 70 |
    | 2 | 88 |
    +----------+------------+

    问题 3:求每个班的平均分

    解:select class_id, avg(score) from student group by class_id;

    +----------+------------+
    | class_id | avg(score) |
    +----------+------------+
    | 1 | 80.0000 |
    | 2 | 93.5000 |
    +----------+------------+

    问题 4:求每个班的总分

    解:select class_id, sum(score) from student group by class_id;

    +----------+------------+
    | class_id | sum(score) |
    +----------+------------+
    | 1 | 160 |
    | 2 | 187 |
    +----------+------------+

  • 相关阅读:
    Mysql如何修改unique key
    centos 编译 安装 protobuf
    EasyNetQ简单使用
    微信发送模板消息
    Python删除开头空格
    代码积累-Common
    sql With(NoLock),With(ReadPast)
    webform 使用log4net配置
    log4net.dll添加报错
    js-小数计算问题
  • 原文地址:https://www.cnblogs.com/wumingoo1/p/14034695.html
Copyright © 2011-2022 走看看