温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MySQL聚合函数的原理及作用

发布时间:2020-06-05 17:14:03 来源:网络 阅读:815 作者:三月 栏目:MySQL数据库

下文我给大家简单讲讲关于MySQL聚合函数的原理及作用,大家之前了解过相关类似主题内容吗?感兴趣的话就一起来看看这篇文章吧,相信看完MySQL聚合函数的原理及作用对大家多少有点帮助吧。 

以下是我们经常要用的一些聚合函数,请谨慎使用,注意sql_mode 模式设置对查询结果的影响,如果sql_mode=' ',那么:
select create_time,test_name,max(moneys) from test_table group by test_name;  查询不报错,但可能与预想结果不一样,时间与最大值不匹配,之前见有开发这样写过,如果sql_mode=‘only_full_group_by’,以上sql 会报错。

  1. avg()   求平均值
    select test_name,avg(moneys) from test_table group by test_name;       按人名分组求平均金额
    +-----------+---------------+
    | test_name | avg(moneys)   |
    +-----------+---------------+
    | 哈罗德    | 550000.175000 |
    | 格温      | 170015.130000 |
    | 班尼      | 915016.630000 |
    +-----------+---------------+

  2. max()   求最大值
    select test_name,max(moneys) from test_table group by test_name;       按人名分组求最大金额
    +-----------+-------------+
    | test_name | max(moneys) |
    +-----------+-------------+
    | 哈罗德    |  1000000.23 |
    | 格温      |   170030.13 |
    | 班尼      |  1660003.13 |
    +-----------+-------------+

  3. min()   求最小值
    select test_name,min(moneys) from test_table group by test_name;        按人名分组求最小金额
    +-----------+-------------+
    | test_name | min(moneys) |
    +-----------+-------------+
    | 哈罗德    |   100000.12 |
    | 格温      |   170000.13 |
    | 班尼      |   170030.13 |
    +-----------+-------------+

  4. sum()   求和
    select test_name,sum(moneys) from test_table group by test_name;       按人名分组求总金额
    +-----------+-------------+
    | test_name | sum(moneys) |
    +-----------+-------------+
    | 哈罗德    |  1100000.35 |
    | 格温      |   340030.26 |
    | 班尼      |  1830033.26 |
    +-----------+-------------+

  5. count() 求行数
    select count() from test_table;     直接求总行数
    +----------+
    | count() |
    +----------+
    |        6 |
    +----------+

select test_name,count() from test_table group by test_name;         按人名分组求频率
+-----------+----------+
| test_name | count() |
+-----------+----------+
| 哈罗德    |        2 |
| 格温      |        2 |
| 班尼      |        2 |
+-----------+----------+

select test_name,count(distinct test_name) from test_table group by test_name;        先去重,后按人名分组求频率
+-----------+---------------------------+
| test_name | count(distinct test_name) |
+-----------+---------------------------+
| 哈罗德    |                         1 |
| 格温      |                         1 |
| 班尼      |                         1 |
+-----------+---------------------------+

6.group_concat()  拼接数据
select test_name,group_concat(test_id),avg(moneys) from test_table group by test_name;  按人名分组拼接id    
+-----------+-----------------------+---------------+
| test_name | group_concat(test_id) | avg(moneys)   |
+-----------+-----------------------+---------------+
| 哈罗德    | 1,2                   | 550000.175000 |
| 格温      | 3,5                   | 170015.130000 |
| 班尼      | 4,6                   | 915016.630000 |
+-----------+-----------------------+---------------+

7.计算每日访问量
select * from t1;
+------+-------+------+
| year | month | day  |
+------+-------+------+
| 2000 |    01 |   01 |
| 2000 |    01 |   20 |
| 2000 |    01 |   30 |
| 2000 |    02 |   02 |
| 2000 |    02 |   23 |
| 2000 |    02 |   23 |
+------+-------+------+

SELECT year,month,BIT_COUNT(BIT_OR(1<<day)) AS days FROM t1
GROUP BY year,month;
+------+-------+------+
| year | month | days |
+------+-------+------+
| 2000 |    01 |    3 |
| 2000 |    02 |    2 |
+------+-------+------+

大家觉得MySQL聚合函数的原理及作用这篇文章怎么样,是否有所收获。如果想要了解更多相关,可以继续关注我们的行业资讯板块。


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI