• grey927
    2019-07-04
    ORDER BY 是对分的组排序还是对分组中的记录排序呢?

    作者回复: 这是个好问题,ORDER BY就是对记录进行排序。如果你在前面用到了GROUP BY,实际上是一种分组的聚合方式,已经把一组的数据聚合成为了一条记录,所以再进行排序的时候,也相当于是对分的组进行排序。

    
     22
  • Amanda
    2019-06-28
    一个发现:虽然 SELECT 的执行顺序在 GROUP BY 和 HAVING 后面,但对于SELECT中列的别名都可以使用。
    MySQL中
    1. > SELECT COUNT(*) as num, role_main, AVG(hp_max) FROM heros
        -> WHERE hp_max>6000
        -> GROUP BY role_main
        -> HAVING COUNT(*)>5
        -> ORDER BY COUNT(*) DESC;
    +-----+-----------+-------------+
    | num | role_main | AVG(hp_max) |
    +-----+-----------+-------------+
    | 17 | 战士 | 7028 |
    | 10 | 坦克 | 8312.4 |
    | 6 | 法师 | 6417 |
    +-----+-----------+-------------+
    2. > SELECT COUNT(*) num, ROUND(AVG(hp_max+mp_max), 2) avg, ROUND(MAX(hp_max+mp_max), 2) max, ROUND(MIN(hp_max+mp_max), 2) min FROM heros
        -> WHERE (hp_max+mp_max)>7000
        -> GROUP BY attack_range
        -> ORDER BY num DESC;
    +-----+---------+----------+---------+
    | num | avg | max | min |
    +-----+---------+----------+---------+
    | 36 | 8654.42 | 11036.00 | 7117.00 |
    | 26 | 7743.77 | 8737.00 | 7025.00 |
    +-----+---------+----------+---------+
    展开

    作者回复: 在执行顺序上,SELECT字段在GROUP BY和HAVING之后,不过在SELECT字段之前,已经计算了聚集函数,也就是COUNT(*) as num。聚集函数的计算在GROUP BY之后,HAVING之前

    
     15
  • ack
    2019-06-28
    练习题
    1.SELECT COUNT(*) AS num,role_main,AVG(hp_max) FROM heros WHERE hp_max > 6000 GROUP BY role_main HAVING num>5 ORDER BY num DESC;
    2.SELECT COUNT(*) AS num,ROUND(MAX(hp_max+mp_max),2),ROUND(AVG(hp_max+mp_max),2),ROUND(MIN(hp_max+mp_max),2) FROM heros WHERE hp_max+mp_max > 7000 GROUP BY attack_range ORDER BY num DESC;
    展开

    作者回复: 正确

     1
     15
  • 安静的boy
    2019-06-28
    where先对数据进行排序,group by再进行分组。让我对数据筛选和分组恍然大悟!

    作者回复: 理解了HAVING和WHERE的区别,就了解了分组过滤和条件过滤。还有SELECT语句种的关键字的顺序:SELECT ... FROM ... WHERE ... GROUP BY ... HAVING ... ORDER BY ...

    
     3
  • 吃饭饭
    2019-06-28
    讲的很详细了,入门必备

    作者回复: 谢谢!

    
     2
  • mickey
    2019-06-28
    /*
    1.筛选最大生命值大于6000的英雄,按照主要定位进行分组,选择分组英雄数量大于5的分组,
    按照分组英雄数从高到低进行排序,并显示每个分组的英雄数量、主要定位和平均最大生命值。
    */
    SELECT count(*) as num, role_main, AVG(hp_max)
    FROM heros
    WHERE hp_max > 6000
    GROUP BY role_main
    HAVING num > 5
    ORDER BY num DESC

    num role_main AVG(hp_max)
    ------------------------------------
    17    战士    7028
    10    坦克    8312.4
    6    法师    6417

    /*
    2.筛选最大生命值与最大法力值之和大于7000的英雄,按照攻击范围来进行分组,
    显示分组的英雄数量,以及分组英雄的最大生命值与法力值之和的平均值、最大值和最小值,
    并按照分组英雄数从高到低进行排序,其中聚集函数的结果包括小数点后两位。
    */
    SELECT count(*) as num, ROUND(AVG(hp_max + mp_max), 2), MAX(hp_max + mp_max), MIN(hp_max + mp_max)
    FROM heros
    WHERE hp_max + mp_max > 7000
    GROUP BY attack_range
    HAVING num > 5
    ORDER BY num DESC

    num, ROUND(AVG(hp_max + mp_max), 2), MAX(hp_max + mp_max), MIN(hp_max + mp_max)
    ------------------------------------------------------------------------
    62    8272.53    11036    7025
    展开

    作者回复: SQL正确,最后结果贴的不太完整

     3
     2
  • Taozi
    2019-06-28
    练习2中反复出现的hp_max+mp_max可以绑定到一个变量吗?
    
     2
  • 圆子蛋
    2019-06-28
    1.SELECT COUNT(*) as num,role_main,AVG(hp_max) FROM heros WHERE hp_max > 6000 GROUP BY role_main HAVING num>5 ORDER BY num DESC;
    2.SELECT COUNT(*) as num,ROUND(MAX(hp_max+mp_max),2),ROUND(AVG(hp_max+mp_max),2),ROUND(MIN(hp_max+mp_max),2) FROM heros WHERE (hp_max+mp_max) > 7000 GROUP BY attack_range ORDER BY num DESC;
    老师在“如何对数据进行分组,并进行聚集统计”的第三个例子里,COUNT(*) 后面没有加 as num,但是 ORDER BY 里直接出现了 num?
    展开

    作者回复: COUNT(*)后面应该有 as num

    
     2
  • mickey
    2019-06-28
    有个错误:
    文中“比如,我们想要按照英雄的主要定位、次要定位进行分组,查看这些英雄的数量,并按照这些分组的英雄数量从高到低进行排序。”的SQL语句:SQL: SELECT COUNT(*), role_main, role_assist FROM heros GROUP BY role_main, role_assist ORDER BY num DESC

    在MySQL里会报错:[Err] 1054 - Unknown column 'num' in 'order clause'

    要改为:SELECT COUNT(*) as num, role_main, role_assist FROM heros GROUP BY role_main, role_assist ORDER BY num DESC;
    展开

    编辑回复: 您好,文章已进行更正,谢谢您的反馈。

    
     2
  • 丁丁历险记
    2019-11-07
    讲个段子 having 常用来做过滤掉那些跑来冒充程序员的人。
    他们深深的震惊了我的认知。

    作者回复: 哈哈

    
     1
  • 峻铭
    2019-09-01
    前面老师在评论中回复过,在group by分组和having筛选分组之间还有一步使用聚集函数进行计算,在目前看到的having都是对cout聚集函数结果的筛选,想试试对其他聚集函数的筛选,然后对训练1做了点小改动:
    select count(*) as c,role_main,avg(hp_max) as v from heros where hp_max > 6000 GROUP BY role_main HAVING c > 5 and v > 7000 order by c DESC;

    作者回复: Good Job

    
     1
  • bear
    2019-08-17
    Having 部分精彩,赞👍

    作者回复: 感谢

    
     1
  • supermouse
    2019-06-29
    思考题 1:
    SELECT
        COUNT(*) AS num, role_main, AVG(hp_max)
    FROM
        heros
    WHERE
        hp_max > 6000
    GROUP BY role_main
    HAVING num > 5
    ORDER BY num DESC;
    思考题 2:
    SELECT
        COUNT(*) AS num,
        attack_range,
        ROUND(AVG(hp_max + mp_max), 2),
        ROUND(MAX(hp_max + mp_max), 2),
        ROUND(MIN(hp_max + mp_max), 2)
    FROM
        heros
    WHERE
        hp_max + mp_max > 7000
    GROUP BY attack_range
    ORDER BY num DESC;
    展开

    作者回复: 正确

    
     1
  • 太精
    2019-06-28
    SELECT COUNT(*) AS num, role_main, AVG(hp_max) AS avg_max FROM heros WHERE hp_max > 6000 GROUP BY role_main HAVING num > 5 ORDER BY num DESC;
    SELECT ROUND((COUNT(*)),2) AS num, ROUND((AVG(hp_max+mp_max)),2) AS heros_avg, ROUND((MAX(hp_max+mp_max)),2) AS max_avg, ROUND((MIN(hp_max+mp_max)),2) AS min_avg FROM heros WHERE (hp_max+mp_max) > 7000 GROUP BY attack_range ORDER BY num desc;

    作者回复: 正确 不过COUNT(*)就不需要用ROUND了

    
     1
  • 大猫当了个当
    2020-02-10
    练习题
    SELECT COUNT(*) as sum, role_main, AVG(hp_max) FROM heros WHERE hp_max > 6000 GROUP BY role_main HAVING sum > 5 ORDER BY sum DESC

    SELECT COUNT(*) as num, ROUND(AVG(hp_max+mp_max),2), ROUND(MAX(hp_max+mp_max),2), ROUND(MIN(hp_max+mp_max),2) FROM heros WHERE hp_max+mp_max > 7000 GROUP BY attack_range ORDER BY num DESC
    展开
    
    
  • LYEMOML
    2020-01-20
    ```sql
    SELECT COUNT(*) as count, AVG(hp_max), role_main FROM heros WHERE hp_max > 6000 GROUP BY role_main HAVING count > 5 ORDER BY count desc;
    ```

    ```sql
    SELECT COUNT(*) as count, ROUND(AVG(hp_max+mp_max), 2), ROUND(MAX(hp_max+mp_max), 2), ROUND(MIN(hp_max+mp_max), 2), attack_range FROM heros WHERE (hp_max+mp_max) > 7000 GROUP BY attack_range ORDER BY count DESC;
    ```
    展开
    
    
  • 爬行的蜗牛
    2019-12-24
    SELECT COUNT(*) as num, role_main, avg(hp_max)
    FROM heros
    WHERE hp_max > 6000
    GROUP BY role_main HAVING num > 5 ORDER BY num DESC;

    作者回复: Good Job

    
    
  • amor
    2019-12-22
    SELECT COUNT(*) AS num,role_main,AVG(hp_max) FROM heros WHERE hp_max >6000 GROUP BY role_main HAVING num >5 ORDER BY num DESC;
    +-----+-----------+-------------+
    | num | role_main | AVG(hp_max) |
    +-----+-----------+-------------+
    | 17 | 战士 | 7028 |
    | 10 | 坦克 | 8312.4 |
    | 6 | 法师 | 6417 |
    +-----+-----------+-------------+
    3 rows in set (0.02 sec)
    展开

    作者回复: 问题1回答正确

    
    
  • 高泽林
    2019-12-14
    非常好!适合我!

    作者回复: 加油~

    
    
  • taoist
    2019-12-12
    # MariaDB:

    1. SELECT role_main,AVG(hp_max) as avg_hp, COUNT(*) AS count FROM heros WHERE hp_max > 6000 GROUP BY role_main HAVING count > 5 ORDER BY count DESC;

    2. SELECT COUNT(*) as num, ROUND(AVG(hp_max+mp_max),2), ROUND( MAX(hp_max+mp_max), 2), ROUND(MIN(hp_max+mp_max) ,2) FROM heros WHERE (hp_max + mp_max) > 7000 GROUP BY attack_range ORDER BY num DESC;
    展开

    作者回复: SQL正确

    
    
我们在线,来聊聊吧