1. 程式人生 > 實用技巧 >MySQL之分組查詢(DQL)

MySQL之分組查詢(DQL)

分組函式

介紹:

  分組函式作用於一組資料,並對一組資料返回一個值,用作統計使用,又稱為聚合函式或統計函式或組函式。

語法:

  SELECT 查詢的欄位,分組函式 FROMGROUP BY 分組的欄位 ORDER BY 排序的欄位;

特點:

  1.可以按單個欄位分組。

  2.和分組函式一同查詢的欄位最好是分組後的欄位。

  3.分組篩選

    分組前篩選:

      針對的表:原始表

      位置:group by的前面

      關鍵字:where

    分組後篩選:

      針對的表:分組後的結果集

      位置:group by的後面

      關鍵字:

having

  4.可以按多個欄位分組,欄位之間用逗號隔開。

  5.可以支援排序。

  6.having後可以支援別名。

組函式型別:

  AVG():平均值,一般用於處理數值型,忽略null值。

  COUNT():計數,可以處理任何型別,忽略null值。

  MAX():最大值,可以處理任何型別,忽略null值。

  MIN():最小值,可以處理任何型別,忽略null值。

  SUM():求和,一般用於處理數值型,忽略null值。

AVG(平均值)和SUM(合計)函式

介紹:

  可以對數值型資料使用AVGSUM函式。

示例:

SELECT AVG(salary),MAX
(salary),MIN(salary),SUM(salary) FROM employees WHERE job_id LIKE '%REP%';

MIN(最小值)和 MAX(最大值)函式

介紹:

  可以對任意資料型別的資料使用MINMAX函式。

示例:

SELECT MIN(hiredate),MAX(hiredate) FROM employees;

COUNT(計數)函式

介紹:

  COUNT(*):返回表中記錄總數,適用於任意資料型別。

  COUNT(expr):返回expr不為空的記錄總數。

示例:

SELECT COUNT(*) FROM employees WHERE
department_id = 50; SELECT COUNT(commission_pct) FROM employees WHERE department_id = 50;

分組資料GROUP BY

介紹:

  可以使用GROUP BY子句將表中的資料分成若干組。

1.在SELECT列表中所有未包含在組函式中的列都應該包含在GROUP BY子句中。

示例:

SELECT department_id,AVG(salary) FROM employees GROUP BY department_id;

2.包含在GROUP BY子句中的列不必包含在SELECT列表中。

示例:

SELECT AVG(salary) FROM employees GROUP BY department_id;

3.使用多個列分組,在GROUP BY子句中包含多個列。

示例:

SELECT department_id dept_id, job_id, SUM(salary) FROM employees GROUP BY department_id, job_id;

非法使用組函式

介紹:

  1.不能在WHERE子句中使用組函式。

  2.可以在HAVING子句中使用組函式。

示例:

SELECT department_id, AVG(salary) FROM employees WHERE AVG(salary) > 8000 GROUP BY department_id;

過濾分組HAVING

介紹:

  使用HAVING過濾分組:

    1.行已經被分組。

    2.使用了組函式。

    3.滿足HAVING子句中條件的分組將被顯示。

示例:

SELECT department_id, MAX(salary) FROM employees GROUP BY department_id HAVING MAX(salary)>10000;

案例講解

#1.簡單的分組
#案例1:查詢每個工種的員工平均工資
SELECT AVG(salary),job_id FROM employees GROUP BY job_id;
#案例2:查詢每個位置的部門個數
SELECT COUNT(*),location_id FROM departments GROUP BY location_id;


#2.可以實現分組前的篩選
#案例1:查詢郵箱中包含a字元的每個部門的最高工資
SELECT MAX(salary),department_id FROM employees WHERE email LIKE '%a%' GROUP BY department_id;
#案例2:查詢有獎金的每個領導手下員工的平均工資
SELECT AVG(salary),manager_id FROM employees WHERE commission_pct IS NOT NULL GROUP BY manager_id;


#3.分組後篩選
#案例:查詢哪個部門的員工個數>5
#①查詢每個部門的員工個數
SELECT COUNT(*),department_id FROM employees GROUP BY department_id;
#②篩選剛才①結果
SELECT COUNT(*),department_id FROM employees GROUP BY department_id HAVING COUNT(*)>5;
#案例2:每個工種有獎金的員工的最高工資>12000的工種編號和最高工資
SELECT job_id,MAX(salary) FROM employees WHERE commission_pct IS NOT NULL GROUP BY job_id HAVING MAX(salary)>12000;
#案例3:領導編號>102的每個領導手下的最低工資大於5000的領導編號和最低工資manager_id>102
SELECT manager_id,MIN(salary) FROM employees GROUP BY manager_id HAVING MIN(salary)>5000;


#4.新增排序
#案例:每個工種有獎金的員工的最高工資>6000的工種編號和最高工資,按最高工資升序
SELECT job_id,MAX(salary) m FROM employees WHERE commission_pct IS NOT NULL GROUP BY job_id HAVING m>6000 ORDER BY m;


#5.按多個欄位分組
#案例:查詢每個工種每個部門的最低工資,並按最低工資降序
SELECT MIN(salary),job_id,department_id FROM employees GROUP BY department_id,job_id ORDER BY MIN(salary) DESC;


#6.簡單的使用
SELECT SUM(salary) FROM employees;
SELECT AVG(salary) FROM employees;
SELECT MIN(salary) FROM employees;
SELECT MAX(salary) FROM employees;
SELECT COUNT(salary) FROM employees;
SELECT SUM(salary) 和,AVG(salary) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 個數 FROM employees;
SELECT SUM(salary) 和,ROUND(AVG(salary),2) 平均,MAX(salary) 最高,MIN(salary) 最低,COUNT(salary) 個數 FROM employees;


#7.引數支援哪些型別
SELECT SUM(last_name) ,AVG(last_name) FROM employees;
SELECT SUM(hiredate) ,AVG(hiredate) FROM employees;
SELECT MAX(last_name),MIN(last_name) FROM employees;
SELECT MAX(hiredate),MIN(hiredate) FROM employees;
SELECT COUNT(commission_pct) FROM employees;
SELECT COUNT(last_name) FROM employees;


#8.是否忽略null
SELECT SUM(commission_pct) ,AVG(commission_pct),SUM(commission_pct)/35,SUM(commission_pct)/107 FROM employees;
SELECT MAX(commission_pct) ,MIN(commission_pct) FROM employees;
SELECT COUNT(commission_pct) FROM employees;
SELECT commission_pct FROM employees;


#9.和distinct搭配
SELECT SUM(DISTINCT salary),SUM(salary) FROM employees;
SELECT COUNT(DISTINCT salary),COUNT(salary) FROM employees;


#10、count函式的詳細介紹
SELECT COUNT(salary) FROM employees;
-- 效率:
-- MYISAM儲存引擎下,COUNT(*)的效率高
-- INNODB儲存引擎下,COUNT(*)和COUNT(1)的效率差不多,比COUNT(欄位)要高一些
SELECT COUNT(*) FROM employees;
SELECT COUNT(1) FROM employees;


#11.和分組函式一同查詢的欄位有限制
SELECT AVG(salary),employee_id FROM employees;


#12.查詢員工最高工資和最低工資的差距(DIFFERENCESELECT MAX(salary)-MIN(salary) DIFFRENCE FROM employees;


#13.查詢各個管理者手下員工的最低工資,其中最低工資不能低於6000,沒有管理者的員工不計算在內
SELECT MIN(salary),manager_id FROM employees WHERE manager_id IS NOT NULL GROUP BY manager_id HAVING MIN(salary)>=6000;


#14.查詢所有部門的編號,員工數量和工資平均值,並按平均工資降序
SELECT department_id,COUNT(*),AVG(salary) a FROM employees GROUP BY department_id ORDER BY a DESC;


#15.選擇具有各個job_id的員工人數
SELECT COUNT(*) 個數,job_id FROM employees GROUP BY job_id;

#16.查詢各job_id的員工工資的最大值,最小值,平均值,總和,並按job_id升序
SELECT MAX(salary),MIN(salary),AVG(salary),SUM(salary),job_id FROM employees GROUP BY job_id


#17.查詢公司員工工資的最大值,最小值,平均值,總和
SELECT MAX(salary) 最大值,MIN(salary) 最小值,AVG(salary) 平均值,SUM(salary) 和 FROM employees;


#18.查詢員工表中的最大入職時間和最小入職時間的相差天數(DIFFRENCE)
SELECT MAX(hiredate) 最大,MIN(hiredate) 最小,(MAX(hiredate)-MIN(hiredate))/1000/3600/24 DIFFRENCE FROM employees;
SELECT DATEDIFF(MAX(hiredate),MIN(hiredate)) DIFFRENCE FROM employees;
SELECT DATEDIFF('1995-2-7','1995-2-6');


#19.查詢部門編號為90的員工個數
SELECT COUNT(*) FROM employees WHERE department_id = 90;

測試資料

#員工表
CREATE TABLE `employees` (
  `employee_id` int(6) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(20) DEFAULT NULL,
  `last_name` varchar(25) DEFAULT NULL,
  `email` varchar(25) DEFAULT NULL,
  `phone_number` varchar(20) DEFAULT NULL,
  `job_id` varchar(10) DEFAULT NULL,
  `salary` double(10,2) DEFAULT NULL,
  `commission_pct` double(4,2) DEFAULT NULL,
  `manager_id` int(6) DEFAULT NULL,
  `department_id` int(4) DEFAULT NULL,
  `hiredate` datetime DEFAULT NULL,
  PRIMARY KEY (`employee_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

#員工資料
INSERT INTO employees VALUES ('100', 'Steven', 'K_ing', 'SKING', '515.123.4567', 'AD_PRES', '24000.00', null, null, '90', '1992-04-03 00:00:00');
INSERT INTO employees VALUES ('101', 'Neena', 'Kochhar', 'NKOCHHAR', '515.123.4568', 'AD_VP', '17000.00', null, '100', '90', '1992-04-03 00:00:00');
INSERT INTO employees VALUES ('102', 'Lex', 'De Haan', 'LDEHAAN', '515.123.4569', 'AD_VP', '17000.00', null, '100', '90', '1992-04-03 00:00:00');
INSERT INTO employees VALUES ('103', 'Alexander', 'Hunold', 'AHUNOLD', '590.423.4567', 'IT_PROG', '9000.00', null, '102', '60', '1992-04-03 00:00:00');
INSERT INTO employees VALUES ('104', 'Bruce', 'Ernst', 'BERNST', '590.423.4568', 'IT_PROG', '6000.00', null, '103', '60', '1992-04-03 00:00:00');
INSERT INTO employees VALUES ('105', 'David', 'Austin', 'DAUSTIN', '590.423.4569', 'IT_PROG', '4800.00', null, '103', '60', '1998-03-03 00:00:00');
INSERT INTO employees VALUES ('106', 'Valli', 'Pataballa', 'VPATABAL', '590.423.4560', 'IT_PROG', '4800.00', null, '103', '60', '1998-03-03 00:00:00');
INSERT INTO employees VALUES ('107', 'Diana', 'Lorentz', 'DLORENTZ', '590.423.5567', 'IT_PROG', '4200.00', null, '103', '60', '1998-03-03 00:00:00');
INSERT INTO employees VALUES ('108', 'Nancy', 'Greenberg', 'NGREENBE', '515.124.4569', 'FI_MGR', '12000.00', null, '101', '100', '1998-03-03 00:00:00');
INSERT INTO employees VALUES ('109', 'Daniel', 'Faviet', 'DFAVIET', '515.124.4169', 'FI_ACCOUNT', '9000.00', null, '108', '100', '1998-03-03 00:00:00');
INSERT INTO employees VALUES ('110', 'John', 'Chen', 'JCHEN', '515.124.4269', 'FI_ACCOUNT', '8200.00', null, '108', '100', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('111', 'Ismael', 'Sciarra', 'ISCIARRA', '515.124.4369', 'FI_ACCOUNT', '7700.00', null, '108', '100', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('112', 'Jose Manuel', 'Urman', 'JMURMAN', '515.124.4469', 'FI_ACCOUNT', '7800.00', null, '108', '100', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('113', 'Luis', 'Popp', 'LPOPP', '515.124.4567', 'FI_ACCOUNT', '6900.00', null, '108', '100', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('114', 'Den', 'Raphaely', 'DRAPHEAL', '515.127.4561', 'PU_MAN', '11000.00', null, '100', '30', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('115', 'Alexander', 'Khoo', 'AKHOO', '515.127.4562', 'PU_CLERK', '3100.00', null, '114', '30', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('116', 'Shelli', 'Baida', 'SBAIDA', '515.127.4563', 'PU_CLERK', '2900.00', null, '114', '30', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('117', 'Sigal', 'Tobias', 'STOBIAS', '515.127.4564', 'PU_CLERK', '2800.00', null, '114', '30', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('118', 'Guy', 'Himuro', 'GHIMURO', '515.127.4565', 'PU_CLERK', '2600.00', null, '114', '30', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('119', 'Karen', 'Colmenares', 'KCOLMENA', '515.127.4566', 'PU_CLERK', '2500.00', null, '114', '30', '2000-09-09 00:00:00');
INSERT INTO employees VALUES ('120', 'Matthew', 'Weiss', 'MWEISS', '650.123.1234', 'ST_MAN', '8000.00', null, '100', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('121', 'Adam', 'Fripp', 'AFRIPP', '650.123.2234', 'ST_MAN', '8200.00', null, '100', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('122', 'Payam', 'Kaufling', 'PKAUFLIN', '650.123.3234', 'ST_MAN', '7900.00', null, '100', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('123', 'Shanta', 'Vollman', 'SVOLLMAN', '650.123.4234', 'ST_MAN', '6500.00', null, '100', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('124', 'Kevin', 'Mourgos', 'KMOURGOS', '650.123.5234', 'ST_MAN', '5800.00', null, '100', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('125', 'Julia', 'Nayer', 'JNAYER', '650.124.1214', 'ST_CLERK', '3200.00', null, '120', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('126', 'Irene', 'Mikkilineni', 'IMIKKILI', '650.124.1224', 'ST_CLERK', '2700.00', null, '120', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('127', 'James', 'Landry', 'JLANDRY', '650.124.1334', 'ST_CLERK', '2400.00', null, '120', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('128', 'Steven', 'Markle', 'SMARKLE', '650.124.1434', 'ST_CLERK', '2200.00', null, '120', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('129', 'Laura', 'Bissot', 'LBISSOT', '650.124.5234', 'ST_CLERK', '3300.00', null, '121', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('130', 'Mozhe', 'Atkinson', 'MATKINSO', '650.124.6234', 'ST_CLERK', '2800.00', null, '121', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('131', 'James', 'Marlow', 'JAMRLOW', '650.124.7234', 'ST_CLERK', '2500.00', null, '121', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('132', 'TJ', 'Olson', 'TJOLSON', '650.124.8234', 'ST_CLERK', '2100.00', null, '121', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('133', 'Jason', 'Mallin', 'JMALLIN', '650.127.1934', 'ST_CLERK', '3300.00', null, '122', '50', '2004-02-06 00:00:00');
INSERT INTO employees VALUES ('134', 'Michael', 'Rogers', 'MROGERS', '650.127.1834', 'ST_CLERK', '2900.00', null, '122', '50', '2002-12-23 00:00:00');
INSERT INTO employees VALUES ('135', 'Ki', 'Gee', 'KGEE', '650.127.1734', 'ST_CLERK', '2400.00', null, '122', '50', '2002-12-23 00:00:00');
INSERT INTO employees VALUES ('136', 'Hazel', 'Philtanker', 'HPHILTAN', '650.127.1634', 'ST_CLERK', '2200.00', null, '122', '50', '2002-12-23 00:00:00');
INSERT INTO employees VALUES ('137', 'Renske', 'Ladwig', 'RLADWIG', '650.121.1234', 'ST_CLERK', '3600.00', null, '123', '50', '2002-12-23 00:00:00');
INSERT INTO employees VALUES ('138', 'Stephen', 'Stiles', 'SSTILES', '650.121.2034', 'ST_CLERK', '3200.00', null, '123', '50', '2002-12-23 00:00:00');
INSERT INTO employees VALUES ('139', 'John', 'Seo', 'JSEO', '650.121.2019', 'ST_CLERK', '2700.00', null, '123', '50', '2002-12-23 00:00:00');
INSERT INTO employees VALUES ('140', 'Joshua', 'Patel', 'JPATEL', '650.121.1834', 'ST_CLERK', '2500.00', null, '123', '50', '2002-12-23 00:00:00');
INSERT INTO employees VALUES ('171', 'William', 'Smith', 'WSMITH', '011.44.1343.629268', 'SA_REP', '7400.00', '0.15', '148', '80', '2014-03-05 00:00:00');
INSERT INTO employees VALUES ('172', 'Elizabeth', 'Bates', 'EBATES', '011.44.1343.529268', 'SA_REP', '7300.00', '0.15', '148', '80', '2014-03-05 00:00:00');
INSERT INTO employees VALUES ('173', 'Sundita', 'Kumar', 'SKUMAR', '011.44.1343.329268', 'SA_REP', '6100.00', '0.10', '148', '80', '2014-03-05 00:00:00');
INSERT INTO employees VALUES ('201', 'Michael', 'Hartstein', 'MHARTSTE', '515.123.5555', 'MK_MAN', '13000.00', null, '100', '20', '2016-03-03 00:00:00');
INSERT INTO employees VALUES ('202', 'Pat', 'Fay', 'PFAY', '603.123.6666', 'MK_REP', '6000.00', null, '201', '20', '2016-03-03 00:00:00');
INSERT INTO employees VALUES ('203', 'Susan', 'Mavris', 'SMAVRIS', '515.123.7777', 'HR_REP', '6500.00', null, '101', '40', '2016-03-03 00:00:00');
INSERT INTO employees VALUES ('204', 'Hermann', 'Baer', 'HBAER', '515.123.8888', 'PR_REP', '10000.00', null, '101', '70', '2016-03-03 00:00:00');
INSERT INTO employees VALUES ('205', 'Shelley', 'Higgins', 'SHIGGINS', '515.123.8080', 'AC_MGR', '12000.00', null, '101', '110', '2016-03-03 00:00:00');
INSERT INTO employees VALUES ('206', 'William', 'Gietz', 'WGIETZ', '515.123.8181', 'AC_ACCOUNT', '8300.00', null, '205', '110', '2016-03-03 00:00:00');