Wu Yuxiong - natural born MySQL study notes: MySQL GROUP BY statement

GROUP BY statement grouping result set according to one or more columns. 
On the grouped columns we can use COUNT, SUM, AVG, and other functions. 
GROUP BY syntax of 
the SELECT column_name, function (column_name) 
the FROM table_name 
the WHERE column_name operator value 
GROUP BY column_name; 
use the following table structure and data, before we can use the following data into the database first. 
NAMES UTF8 the SET; 
the SET FOREIGN_KEY_CHECKS = 0; 
the DROP TABLE `employee_tbl` the IF EXISTS; 
the CREATE TABLE employee_tbl`` ( 
  `id` int ( . 11 ) the NOT NULL, 
  ` char name` ( 10) the NOT NULL the DEFAULT '' , 
  `date` in the previous datetime NULL the NOT, 
  `singin` tinyint ( . 4) the NOT NULL the DEFAULT ' 0 ' the COMMENT 'Logins ' , 
  a PRIMARY KEY ( `id`) 
) ENGINE = the InnoDB the DEFAULT the CHARSET = UTF8; 

the BEGIN; 
the INSERT the INTO` employee_tbl` the VALUES ( ' . 1 ' , ' Bob ' , ' 2016-04-22 15:25:33 ' , ' 1 ' ), ( ' 2 ' , ' Wang ' , ' 2016-04-20 15:25:47 ' , ' 3 ' ), ( ' 3 ' , ' Mary ' ,'2016-04-19 15:26:02 ' , ' 2 ' ), ( ' 4 ' , ' Wang ' , ' 2016-04-07 15:26:14 ' , ' 4 ' ), ( ' 5 ' , ' Bob ' , ' 2016-04-11 15:26:40 ' , ' 4 ' ), ( ' 6 ' , ' Bob ' , ' 2016-04-04 15:26:54 ' , '2'); 
A COMMIT; 

the SET FOREIGN_KEY_CHECKS =. 1 ; 

the introduction is successful, the following SQL statement: 
MySQL > SET UTF8 names; 
MySQL > the SELECT * the FROM employee_tbl;
 + ---- + ---- + -------- + -------- + ----------------- 
| the above mentioned id | name | DATE | Singin | 
+ ---- + -------- + --------------------- + -------- + 
| 1 | Xiao Ming | 2016-04-22 15:25:33 | 1 | 
| 2 | Amy | 2016-04-20 15:25:47 | 3 | 
| 3 | Mary | 2016-04-19 15:26:02 | 2 | 
| 4 | Amy | 2016-04-07 15 : 26 is: 14 |. 4 | 
|. 5 | Bob | 2016-04-11 15:26:40 |. 4 | 
|. 6 | Bob | 2016-04-04 15:26:54 | 2 | 
+ ---- + - --------------------- + -------- + ------- +
Next, use the GROUP BY statement to the data table are grouped by names and statistics for each person how many records: 
MySQL > the SELECT name, COUNT (* ) GROUP BY employee_tbl the FROM name;
 + -------- + - + -------- 
| name | COUNT (*) | 
+ -------- + ---------- + 
| Mary | 1 | 
| Xiao Ming | 3 | 
| Wang | 2 | 
+ -------- + ---------- +
The ROLLUP using the WITH 
the WITH then the ROLLUP can achieve the same statistical (SUM, AVG, COUNT ...) on the basis of statistical data in the packet. 
MySQL > the SELECT name, SUM (Singin) AS singin_count the FROM employee_tbl the GROUP BY name the WITH ROLLUP;
 + -------- + -------------- + 
| name | singin_count | 
+ + -------------- + -------- 
| Mary | 2 | 
| Xiao Ming | 7 | 
| Amy | 7 | 
| NULL | 16 | 
+ --- ----- + -------------- + 
which represents a NULL record logins for all. 
Can coalesce to set a be substituted NUll name, coalesce syntax: 
SELECT coalesce (A, B, C); 
MySQL > the SELECT coalesce (name, ' Total ' ), the SUM (Singin) AS singin_count the FROM employee_tbl the GROUP BY name the WITH the ROLLUP ;
+ -------------------------- + -------------- + 
| COALESCE (name, ' total ' ) | singin_count | 
+ -------------------------- + -------------- + 
| small Lai | 2 | 
| Xiao Ming | 7 | 
| Amy | 7 | 
| total | 16 | 
+ -------------------------- + - ------------ +

 

Guess you like

Origin www.cnblogs.com/tszr/p/12113811.html