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 |
+------+-------+------+

猜你喜欢

转载自blog.51cto.com/11103985/2327099
0条评论
添加一条新回复