SELECT COUNT(*) FROM T_Employee WHERE FAge=23; //查看人數 ALTER TABLE T_Employee ADD FSubCompany VARCHAR(20); ALTER TABLE T_Employee ADD FDepartment VARCHAR(20); //為T_Employee表增加兩列 UPDATE T_Employee SET FSubCompany='Beijing',FDepartment='Development' WHERE FNumber='DEV001'; //兩個字段添加完畢後還需要將表中原有數據行的這兩個字段值更新
SQL 中提供了聚合函數來完成計算統計結果集條數、某個字段的最大值、某個字段的最小值、某個字段的平均值以及某個字段的合計值等數據統計的功能。
數據分組用來將數據分為多個邏輯組,從而可以對每個組進行聚合運算。SQL語句中使用GROUP BY子句進行分組,使用方式為“GROUP BY 分組字段”。分組語句必須和聚合函數一
起使用,GROUP BY子句負責將數據分成邏輯組,而聚合函數則對每一個組進行統計。
SELECT FAge FROM T_Employee GROUP BY FAge; //查看公司員工有哪些年齡段的 SELECT FAge FROM T_Employee;//查看公司員工所有年齡 SELECT FAge,AVG(FSalary) FROM T_Employee GROUP BY FSubCompany,FDepartment; SELECT FAge,COUNT(*) AS CountOfThisAge FROM T_Employee GROUP BY FAge;//查看每個年齡段的員工的人數 SELECT FSubCompany,avg(FSalary) AS FSalarySUM FROM T_Employee GROUP BY FSubCompany; SELECT FAge,COUNT(*) AS CountOfThisAge FROM T_Employee GROUP BY FAge HAVING COUNT(*)>1; //只檢索人數多余1個的年齡段