MySQL聚合函数的原理和作用
发布时间: 2020-06-05 17:14:03
资料来源: 51CTO
阅读: 278
作者: 3月
下面我将简要介绍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报告错误。 avg ) )求出平均值
select test_name,avg(moneys ) from test_table group by test_name; 按人名分组求出平均金额
--------------------- -
test _ name (moneys ) |
--------------------- -
|无知的葡萄酒| 550000.175000 |
|阮| 170015.130000 |
|缓慢的鸵鸟| 915016.630000 |
--------------------- -
max ) )求出最大值
select test_name,max(moneys ) from test_table group by test_name; 按人名求出最大金额
-----------------
test _ name (moneys )
-----------------
|无知的葡萄酒| 1000000.23 |
|阮| 170030.13 |
|慢鸵鸟| 1660003.13 |
-----------------
min ) )求出最小值
select test_name,min(moneys ) from test_table group by test_name; 按人名分组求出最小金额
-----------------
test _ name (moneys )
-----------------
|无知的葡萄酒| 100000.12 |
|阮| 170000.13 |
|慢鸵鸟| 170030.13 |
-----------------
sum ) )的总和
select test_name,sum(moneys ) from test_table group by test_name; 按人名分组求出合计金额
-----------------
test _ name (moneys )
-----------------
|无知的葡萄酒| 1100000.35 |
|阮| 340030.26 |
|慢鸵鸟| 1830033.26 |
-----------------
count ) )求出行数
选择计数() from test_table; 直接求出总行数
------------
| count () |
------------
| 6 |
------------
选择test _ name,count (from test _ tablegroupbytest _ name; 按人名分组求出频率
-----------------
| test _ name (|
-----------------
无知的葡萄酒
|阮| 2 |
悠闲的鸵鸟| 2 |
-----------------
选择测试_ name,count(distinct test_name ) from test_table group by test_name; 先取重量,按人名分组求出频率
-------------请参阅
test _ name (distinct test _ name ) |
-------------请参阅
无知的葡萄酒
|阮| 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
------------- -请参阅
|无知的葡萄酒| 1,2|550000.175000 |
|阮| 3,5|170015.130000 |
|缓慢的鸵鸟| 4,6|915016.630000 |
------------- -请参阅
7 .计算每日访问量
select * from t1;
-----------------
年/日
-----------------
| 2000 | 01 | 01 |
| 2000 | 01 | 20 |
| 2000 | 01 | 30 |
| 2000 | 02 | 02 |
| 2000 | 02 | 23 |
| 2000 | 02 | 23 |
-----------------
选择年,month,bit_count(bit_or(1
组by year,month;
-----------------
“年度”
-----------------
| 2000 | 01 | 3 |
| 2000 | 02 | 2 |
-----------------
你觉得MySQL聚合函数的原理和作用这篇文章怎么样? 有得到的东西吗? 如果想知道更多的相关性,请继续关注行业信息板块。