數據庫操作(五)

回顧:
1、基本查詢語句
select 字段名1....
from 表名
where 條件

    from 范圍 → where 條件 → select 查詢

2、查詢條件
   
3、order by子句
    排序

正課:
1、聚合函數
查詢時需要做一些數據統計,如:平均值、最大最小值,求和
像這些必須對表中數據進行統計、計算、分析返回一個結果。
這些值是沒有辦法非常直觀的列出,而是根據現有的數據,
經過分析得到相應的結果。

****多行數據,經過統計分析得到一個結果。
因為聚合函數是對于多行數據記錄,進行統計分析返回
一行數據,所以又被稱為:多行函數、集合函數、組函數。

1)、max、min
max(column):對指定字段,進行統計分析,
得到最大值。
min(column):對指定字段,進行統計分析,
得到最小值。

--所有員工的薪資最大值。
select max(sal) from emp;

--30部門員工的薪資最大值。
select max(sal) from emp where deptno = 30;

--30部門最高薪資的員工信息。
select * from emp where sal = (
    --30部門的最高工資
    select max(sal) 
        from emp 
        where deptno = 30
) and deptno = 30;

錯誤的sql-1:
select *,max(sal) from emp where deptno = 30;
分析:
--查詢30部門員工信息(返回多條數據)
select * from emp where deptno = 30;
--查詢30部門最高工資(返回1條數據)
select max(sal) from emp where deptno = 30;


錯誤的sql-2:
分析:
--max(sal)是對員工薪資統計分析之后得到的結果。
--第一次max(sal)必須在select后面。
--即聚合函數不能出現在條件查詢中。
select * from emp 
    where sal = max(sal) 
    and deptno = 30;


錯誤的sql-3:
分析
--薪資等于30部門的最高工資的員工信息
select * from emp where sal = (
    --30部門的最高工資
    select max(sal) 
        from emp 
        where deptno = 30
);

2)、sum、avg
sum(column):對指定字段,進行統計分析
得到總和。
avg(column):得到平均值

--所有員工薪資總和
select sum(sal) 工資總和 from emp;

--30部門的薪資總和
select sum(sal) 30部門工資總和 
    from emp where deptno = 30;

--查詢公司員工平均工資
select avg(sal) 平均工資 from emp;--2073.21
等價于
select sum(sal) / count(sal) from emp;

--將程咬金當成員工進行平均工資統計。
select sum(sal) / count(ename) from emp;

--30部門的平均工資
select avg(sal) "30部門平均工資"
    from emp where deptno = 30;

3)、count
count(column):對指定字段進行計數統計
忽略null值的統計。

--公司中有績效的人數
select count(comm) from emp;

--公司中有工資的人數
select count(sal) from emp;

--統計公司的人數
--只要保證一條記錄中,任意一個字段有值,
--  就算是一條有效的數據記錄,使用count(*)。
select count(*) from emp;

總結:聚合函數對null值的處理:忽略。

--查詢所有員工的最大工資、最小工資、
--  平均工資、工資總和。
select max(sal) 最大工資,min(sal) 最小工資,
    avg(sal) 平均工資,sum(sal) 工資總和
from emp;


練習:
--查詢各部門的最大工資、最小工資、
--  平均工資、工資總和。
如何實現???

如果看到各部門、每個部門如此的字眼,
請一定要考慮到分組操作。

2、group by子句
group by 字段名1,字段名2...
根據指定字段,對數據記錄進行分組操作。

select deptno 部門,max(sal) 最大工資,min(sal) 最小工資,
    round(avg(sal)) 平均工資,sum(sal) 工資總和
from emp
group by deptno;--根據部門編號分組,同一個部門編號的為一組。


from 范圍 → group by 分組 → 查詢 每組的數據。


--查詢每個工資的人數。
select sal 工資,count(*) 人數 from emp 
        where sal is not null
    group by sal;

--查詢每個部門中姓名及工資。
select deptno 部門,ename 姓名,sal 工資
    from emp
    group by deptno;

原因既是結論:
a、當一條SQL語句中,出現了group by分組子句時,
   則select子句中,只能出現在group by后面的字段
    或者是聚合函數。

group by子句意在對數據進行分組操作。
則數據必定是以組為單位進行統計分析。
就無法實現對單條數據做數據顯示操作。

b、分組字段如果為多個,如group by 字段名1,字段名2
    先按字段名1分組,在分組之后結果數據中,
    再按字段名2分組,以此類推。

c、group by子句對應的分組字段,
    必定在數據中擁有重復值,否則分組將無意義。


練習:
--相同工資的人數統計。
select sal 工資,count(*) 人數
    from emp 
        where sal is not null
    group by sal;

--有相同工資的人數的工資及人數。
select sal 工資,count(*) 人數
    from emp 
        where sal is not null
    --and count(*) > 1--where條件查詢中不允許出現聚合函數。
    group by sal;

3、having子句
二次條件過濾
在對數據進行分組后,對分組的結果數據進行二次條件過濾

--有相同工資的人數的工資及人數。
select sal 工資,count(*) 人數
    from emp 
        where sal is not null
    --and count(*) > 1--where條件查詢中不允許出現聚合函數。
    group by sal --已經分組后就可以使用分組函數了
    having count(*) > 1;

注意:
a、having不可能單獨出現,必須跟在group by后面。
b、group by分組后,having中可以出現分組函數。


where
--分組
having

having與where的區別?
a、where對查詢數據進行一次條件過濾。
    where子句中不允許出現分組函數。


b、having對分組后的結果數據進行二次條件過濾。
    having子句中允許出現分組函數。
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容