count |
取得記錄數 |
sum |
求和 |
avg |
取平均 |
max |
取最大的數 |
min |
取最小的數 |
注意:分組函數自動忽略空值,不需要手動的加where條件排除空值。
select count(*) from emp where xxx; 符合條件的所有記錄總數。
select count(comm) from emp; comm這個字段中不為空的元素總數。
注意:分組函數不能直接使用在where關鍵字后面。
mysql> select ename,sal from emp where sal > avg(sal);
ERROR 1111 (HY000): Invalid use of group function
● 取得所有的員工數
select count(*) from emp;
Count(*)表示取得所有記錄,忽略null,為null的值也會取得;
● 取得津貼不為null員工數
select count(comm) from emp;
采用count(字段名稱),不會取得為null的記錄。
● 取得工作崗位的個數
select count(distinct job ) from emp;
可以將這些聚合函數都放到select中一起使用;
select count(*),sum(sal),avg(sal),max(sal),min(sal) from emp;