汇总数据

1.聚集函数

AVG():返回某列的平均值

COUNT():返回某列的行数

                       1. SELECT CONUT(*) AS num_cust FROM table_name;(NULL也要COUNT,对所有行计算)

                        2. SELECT CONUT(column_name1) AS num_cust FROM table_name; (对特定行计数,NULL 会被忽略)

MAX()、MIN():返回某列最大值\最小值:要指定column,忽略值为NULL的行;

SUM():返回某列之和:SELECT SUM(column_name) AS total_column_name FROM table_name WHERE key

key 保证了只对主键sum。(SUM()函数忽略NULL值)

2.聚集不同的值

聚集函数的DISTINCT的使用(只在MySQL5,不能再MySQL4.x中正常运行)

SELECT AVG(DISTINCT column_name1) AS avg_column_name1 FROM table_name WHERE key = xxx;

分析:DISTINCT只考虑不同的column_name1的行值,相同的值将会忽略

NOTE:DISTINCT 后面只能接列名,不能接表达式(*)

3.组合聚集函数

SELECT COUNT(*) AS num_items,MIN(column_name1) AS column_name1_min, MAX(column_name1) AS column_name1_MAX, AVG(column_name1) AS column_name1_avg FROM table_name;

你可能感兴趣的:(汇总数据)