MySQL database_data query_sort, aggregate function, group

MySQL database

Sort

  • In order to facilitate viewing the data, you can sort the data

grammar:

select * from 表名 order by 列1 asc|desc ,列2 asc|desc,...

Description:

  • Sort the row data according to column 1, if some rows and column 1 have the same value, sort according to column 2, and so on
  • By default, sort from small to large by column value (asc)
  • asc sorted from small to large, that is, ascending
  • desc is sorted from largest to smallest, that is, descending

Example 1: Query the information of boys without deleting them, in descending order by student ID

select * from students where gender=1 and is_delete=0 order by id desc;

Example 2: Query the student information that is not deleted, in ascending order by name
order by field

select * from students where is_delete=0 order by name;

order by multiple fields

select * from students where age between 18 and 34 order by height desc,id desc;

Example 3: Display all student information, first sort by age -> younger, when the age is the same, sort by height -> short

select * from students  order by age desc,height desc;

Aggregate function

  • In order to get statistical data quickly, the following 5 aggregation functions are often used

total

  • count(*): Count the total number of rows, write the star and column name in parentheses, the result is the same

Example 1: Query the total number of students

select count(*) from students;

select count(*) as 男性人数 from students where gender=0;

select count(*) as 女性人数 from students where gender=1;

Max

  • max (column) means to find the maximum value of this column

Example 2: Query the maximum number of girls

select max(id) from students where gender=2;

Minimum

  • min (column) means to find the minimum value of this column

Example 3: Query the minimum number of undeleted students

select min(id) from students where is_delete=0;

Sum

  • sum (column) means finding the sum of this column

Example 4: Query the total age of boys

select sum(age) from students where gender=1;

-- 平均年龄
select sum(age)/count(*) from students where gender=1;

average value

  • avg (column) means to find the average of this column

Example 5: Query the average number of girls who have not been deleted

select avg(id) from students where is_delete=0 and gender=2;

Grouping

group by

  • The meaning of group by: group the query results according to one or more fields, the same field value is a group
  • group by can be used to group a single field or multiple fields
select * from students;
+----+-----------+------+--------+--------+--------+-----------+
| id | name      | age  | height | gender | cls_id | is_delete |
+----+-----------+------+--------+--------+--------+-----------+
|  1 | 小明      |   18 | 180.00 | 女     |      1 |           |
|  2 | 小月月    |   18 | 180.00 | 女     |      2 |          |
|  3 | 彭于晏    |   29 | 185.00 | 男     |      1 |           |
|  4 | 刘德华    |   59 | 175.00 | 男     |      2 |          |
|  5 | 黄蓉      |   38 | 160.00 | 女     |      1 |           |
|  6 | 凤姐      |   28 | 150.00 | 保密   |      2 |          |
|  7 | 王祖贤    |   18 | 172.00 | 女     |      1 |          |
|  8 | 周杰伦    |   36 |   NULL | 男     |      1 |           |
|  9 | 程坤      |   27 | 181.00 | 男     |      2 |           |
| 10 | 刘亦菲    |   25 | 166.00 | 女     |      2 |           |
| 11 | 金星      |   33 | 162.00 | 中性   |      3 |          |
| 12 | 静香      |   12 | 180.00 | 女     |      4 |           |
| 13 | 周杰      |   34 | 176.00 | 女     |      5 |           |
| 14 | 郭靖      |   12 | 170.00 | 男     |      4 |           |
+----+-----------+------+--------+--------+--------+-----------+


select gender from students group by gender;
+--------+
| gender |
+--------+
| 男     |
| 女     |
| 中性   |
| 保密   |
+--------+

Grouped according to the gender field, all values ​​of the gender field have 4'male','female','neutral', and'confidential', so they are divided into 4 groups. When group by is used alone, only the number of each group is displayed. One record, so the actual meaning of group by alone is of little use.

group by + group_concat()

  • group_concat (field name) can be used as an output field,
  • After grouping, according to the grouping result, use group_concat() to place the collection of the value of a certain field in each group.
select gender from students group by gender;
+--------+
| gender |
+--------+
| 男     |
| 女     |
| 中性   |
| 保密   |
+--------+


select gender,group_concat(name) from students group by gender;
+--------+-----------------------------------------------------------+
| gender | group_concat(name)                                        |
+--------+-----------------------------------------------------------+
| 男     | 彭于晏,刘德华,周杰伦,程坤,郭靖                                 |
| 女     | 小明,小月月,黄蓉,王祖贤,刘亦菲,静香,周杰                        |
| 中性   | 金星                                                       |
| 保密   | 凤姐                                                       |
+--------+-----------------------------------------------------------+


select gender,group_concat(id) from students group by gender;
+--------+------------------+
| gender | group_concat(id) |
+--------+------------------+
| 男     | 3,4,8,9,14       |
| 女     | 1,2,5,7,10,12,13 |
| 中性   | 11               |
| 保密   | 6                |
+--------+------------------+

group by + aggregate function

  • Inspired by group_concat(), since we can count the set of values ​​of a certain field in each group, we can also use the set function to do some operations on this set of values
select gender,group_concat(age) from students group by gender;
+--------+----------------------+
| gender | group_concat(age)    |
+--------+----------------------+
| 男     | 29,59,36,27,12       |
| 女     | 18,18,38,18,25,12,34 |
| 中性   | 33                   |
| 保密   | 28                   |
+--------+----------------------+


分别统计性别为男/女的人年龄平均值
select gender,avg(age) from students group by gender;
+--------+----------+
| gender | avg(age) |
+--------+----------+
| 男     |  32.6000 |
| 女     |  23.2857 |
| 中性   |  33.0000 |
| 保密   |  28.0000 |
+--------+----------+

分别统计性别为男/女的人的个数
select gender,count(*) from students group by gender;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男     |        5 |
| 女     |        7 |
| 中性   |        1 |
| 保密   |        1 |
+--------+----------+

group by + having

  • having conditional expression: used to specify some conditions after grouping query to output query results
  • The role of having is the same as where, but having can only be used for group by
select gender,count(*) from students group by gender having count(*)>2;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男     |        5 |
| 女     |        7 |
+--------+----------+

group by + with rollup

  • The role of with rollup is to add a new row at the end to record the sum of all records in the current column
select gender,count(*) from students group by gender with rollup;
+--------+----------+
| gender | count(*) |
+--------+----------+
| 男     |        5 |
| 女     |        7 |
| 中性   |        1 |
| 保密   |        1 |
| NULL   |       14 |
+--------+----------+


select gender,group_concat(age) from students group by gender with rollup;
+--------+-------------------------------------------+
| gender | group_concat(age)                         |
+--------+-------------------------------------------+
| 男     | 29,59,36,27,12                            |
| 女     | 18,18,38,18,25,12,34                      |
| 中性   | 33                                        |
| 保密   | 28                                        |
| NULL   | 29,59,36,27,12,18,18,38,18,25,12,34,33,28 |
+--------+-------------------------------------------+

Guess you like

Origin blog.csdn.net/weixin_42250835/article/details/90402449