mysql的聚集函數
avg() count() max() min() sum()
avg() 求一個列的平均值 null會被略過
mysql> SELECT AVG(goods_id) as goods_id from club_goods;
+------------+
| goods_id |
+------------+
| 83987.7589 |
+------------+
1 row in set (0.00 sec)
count() 確定表中行的數據或者符合特定條件的數目 null會被略過 但是空字符串不會被略過
所以 count()裏面可以寫 一個條件判斷語句
mysql> select count(*) as result from test_order;
+----------+
| result |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
max() 返回指定列的最大值 如果是中文的話 則是字符串長度
mysql> select max(goods_id) as goods_id from club_goods;
+----------+
| goods_id |
+----------+
| 100111 |
+----------+
1 row in set (0.00 sec)
min() 返回指定列的最小值
mysql> select min(goods_id) as goods_id from club_goods;
+----------+
| goods_id |
+----------+
| 38 |
+----------+
1 row in set (0.00 sec)
sum() 用來返回指定列值的和 null 略過 字符串的話直接爲0
mysql> select sum(goods_id) from club_goods;
+---------------+
| sum(goods_id) |
+---------------+
| 9406629 |
+---------------+
1 row in set (0.00 sec)
如果是單表 用了聚集函數的就不能在 select 單個字段了 會產生語法錯誤 可以用其他的聚集函數
但是 可以在聚集函數裏面用其他的 算術 或者 選擇器的函數 DISTINCT CONACT() ..