MYSQL-聚合函数

聚合函数

聚合函数aggregation function 可称为组函数, 一般可配合 group byhaving 使用

count 计算行数
mysql> select count(1) from websites;
+----------+
| count(1) |
+----------+
|        6 |
+----------+
--字段去重后计算行数
mysql> select count(distinct country) from websites;
+-------------------------+
| count(distinct country) |
+-------------------------+
|                       2 |
+-------------------------+
avg 平均数
mysql> select avg(alexa) from websites;
+------------+
| avg(alexa) |
+------------+
|    93.3333 |
+------------+
max 最大值
mysql> select max(alexa) from websites;
+------------+
| max(alexa) |
+------------+
|        300 |
+------------+
min 最小值
mysql> select min(alexa) from websites;
+------------+
| min(alexa) |
+------------+
|          1 |
+------------+
sum 求和
mysql> select sum(alexa) from websites;
+------------+
| sum(alexa) |
+------------+
|        560 |
+------------+
mysql> select *, round(alexa/(select sum(alexa) from websites)*100, 2) as percent from websites;
+----+--------------+---------------------------+-------+---------+---------+
| id | name         | url                       | alexa | country | percent |
+----+--------------+---------------------------+-------+---------+---------+
|  1 | Google       | https://www.google.cm/    |     1 | USA     |    0.18 |
|  2 | 淘宝         | https://www.taobao.com/   |    13 | CN      |    2.32 |
|  3 | 菜鸟教程     | http://www.runoob.com/    |   300 | CN      |   53.57 |
|  4 | 微博         | http://weibo.com/         |    20 | CN      |    3.57 |
|  5 | Facebook     | https://www.facebook.com/ |     3 | USA     |    0.54 |
|  6 | 百度         | https://www.baidu.com     |   223 | CN      |   39.82 |
+----+--------------+---------------------------+-------+---------+---------+
查询alexa大于平均值的记录
mysql> select * from websites where alexa > (select avg(alexa) from websites);
+----+--------------+------------------------+-------+---------+
| id | name         | url                    | alexa | country |
+----+--------------+------------------------+-------+---------+
|  3 | 菜鸟教程     | http://www.runoob.com/ |   300 | CN      |
|  6 | 百度         | https://www.baidu.com  |   223 | CN      |
+----+--------------+------------------------+-------+---------+
分组求平均
mysql> select country, avg(alexa) from websites group by country;
+---------+------------+
| country | avg(alexa) |
+---------+------------+
| USA     |     2.0000 |
| CN      |   139.0000 |
+---------+------------+
2 rows in set (0.02 sec)

mysql> select country, avg(alexa) as avg_alexa from websites group by country having avg_alexa > 100;
+---------+-----------+
| country | avg_alexa |
+---------+-----------+
| CN      |  139.0000 |
+---------+-----------+
1 row in set (0.00 sec)
group by 分组
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name[, column_name2, ..., column_nameN];
having 筛选分组后的数据
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name
HAVING aggregate_function(column_name) operator value;
group_concat 分组合并
mysql> select country, group_concat(name) from websites group by country;
+---------+-----------------------------------+
| country | group_concat(name)                |
+---------+-----------------------------------+
| CN      | 淘宝,菜鸟教程,微博,百度           |
| USA     | Google,Facebook                   |
+---------+-----------------------------------+
2 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值