MySQL database basic usage - aggregation - grouping

polymerization

  • In order to get statistics quickly, 5 aggregation functions are provided
  • count(*) means to calculate the total number of rows, write asterisk and column name in parentheses, the result is the same
  • Query the total number of students
select count(*) from students;

  

  • max(column) means to find the maximum value of this column
  • Query the maximum number of girls
select max(id) from students where gender=0;

  

  • min(column) means to find the minimum value of this column
  • Query the minimum number of undeleted students
select min(id) from students where isdelete=0;

 

  • sum(column) means to find the sum of this column
  • After querying the boy's number
select sum(id) from students where gender=1;

  

  • avg(column) means to find the average of this column
  • Query the average number of undeleted girls
select sum(id) from students where gender=1;

  

grouping

  • Group by field, indicating that data with the same field will be put into a group
  • After grouping, only the same data column can be queried, and the different data columns cannot appear in the result set
  • The grouped data can be counted and aggregated
  • grammar
select column 1, column 2, aggregation... from table name group by column 1, column 2, column 3....
  •   Query the total number of men and women
select gender as gender,count(*)

from students

group by gender;

  Query the number of people in each city

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324731188&siteId=291194637