[Turn] [MySQL Query Language] - query group by group

group by

(1) group by meanings: The query results are grouped according to one or more fields, the same field values as a group
(2) group by a single field can be used for the packet, the packet may be a plurality of fields

* SELECT from Employee;
 + ------ + ------ + ------ + + -------- + ------- ------ + ------ 
| NUM | D_ID | name | Age | Sex | homeaddr | 
+ ------ + ------ + ------ + -------- + ------------- + ------ + 
| 1 | 1001 | Joe Smith | 26 | M | beijinghdq | 
| 2 | 1002 | John Doe | 24 | F | beijingcpq | 
| 3 | 1003 | Wang Wu | 25 | M | changshaylq | 
| 4 | 1004 | Aric | 15 | M | England | 
+ ------ + ------ + -------- ------ ------ + ------------- + + + 
 
SELECT * from Employee Group by D_ID, Sex; 
 
SELECT * from Employee by Group Sex;
 + - ---- + -------- + ------ + ------ + ------ + ------------ + 
| num  | d_id | name   | age  | sex  | homeaddr   |
+ - ----- + -------- + ------ + ------ + ------ + ------------ + 
| 2 | 1002 | John Doe | 24 | F | beijingcpq | 
| 1 | 1001 | Joe Smith | 26 | M | beijinghdq | 
+ ------ + ------ + ------ - + ------ + ------ + ------------ + 
the packet fields sex, sex all values only two fields ( 'M' and ' F ' ), it is divided into two groups 
when used alone group by show only the first record in each group 
so little practical significance when used alone group by

group by + group_concat()

(1) group_concat (field names) may be used as an output field,
(2) after the packet, the packet according to the result, using GROUP_CONCAT () to place the value of a field in each group of the set of

select sex from employee group by sex;
+------+
| sex  |
+------+
| 女   |
| 男   |
+------+
 
select sex,group_concat(name) from employee group by sex;
+------+--------------------+
| sex  | group_concat(name) |
+------+--------------------+
| 女   | 李四               |
| 男   | 张三,王五,Aric     |
+------+--------------------+
 
select sex,group_concat(d_id) from employee group by sex;
+------+--------------------+
| sex  | group_concat(d_id) |
+------+--------------------+
| 女   | 1002               |
| 男   | 1001,1003,1004     |
+------+--------------------+

aggregate function group by +

(1) by group_concat () inspired, since we can count a set of values ​​for a field in each packet, then we can do something about this "set of values" by aggregate functions

Sex SELECT, GROUP_CONCAT (Age) from Employee by Group Sex;
 + ------ + ------------------- + 
| Sex | GROUP_CONCAT (Age) | 
+ + ------------------- + ------ 
| female | 24-| 
| M | 26,25,15 | 
+ ------ + - ------------------ + 
 
respectively gender statistics for men / woman who the average age of 
the SELECT sex, AVG (Age) from the Employee Group by sex;
 + ----- - + ---------- + 
| Sex | AVG (Age) | 
+ ------ + ---------- + 
| F | 24.0000 | 
| M | 22.0000 | 
+ ------ + ---------- + 
 
respectively gender statistics for men / woman in the number of people 
the SELECT sex, COUNT (sex) from the Employee Group by sex;
 + ----- - + ------------ + 
| Sex | COUNT (Sex) |  
+ ------ + ------------ +
| F | 1 | 
| M | 3 | 
+ ------ + ------------ +

group by + having

(1) having the conditional expression: the query packet to the output conditions specified number of search results
(2) having the same function and where, but only for the group by HAVING

select sex,count(sex) from employee group by sex having count(sex)>2;
+------+------------+
| sex  | count(sex) |
+------+------------+
| 男   |          3 |
+------+------------+

group by + with rollup

The role of (1) with rollup is: the last new row to record the sum of all the records in the current column

select sex,count(age) from employee group by sex with rollup;
+------+------------+
| sex  | count(age) |
+------+------------+
| 女   |          1 |
| 男   |          3 |
| NULL |          4 |
+------+------------+
 
select sex,group_concat(age) from employee group by sex with rollup;
+------+-------------------+
| sex  | group_concat(age) |
+------+-------------------+
| 女   | 24                |
| 男   | 26,25,15          |
| NULL | 24,26,25,15       |
+------+-------------------+

 

Guess you like

Origin www.cnblogs.com/zouwangblog/p/10984624.html