MySQL group function and group query


Database used- link address

One, grouping function

1. Classification

  • count: get the number of records
  • sum: sum
  • avg: take the average
  • max: take the maximum number
  • min: take the smallest number

2. Overview

  • Grouping function is also called multi-line processing function (input multiple lines, output one line)
  • The grouping function automatically ignores null values, no need to manually add where conditions to exclude null values
  • Grouping functions can be used in combination with each other
  • The grouping function is generally used in conjunction with group by
  • It can be used in combination with distinct (distinct can only appear at the top of all fields, and multiple fields indicate joint deduplication, which is the deduplication of the query result set without changing the table data)
  • The grouping function cannot be used directly after the where statement:
    because any grouping function is executed after the execution of the group by statement, and the group by is executed after the execution of where

Two, count

  • count(*) means to obtain all records, do not ignore the null, that is to obtain the null record; instead of counting the number of data in a field, it is the total number of records (not related to a field)
  • Using count (field name), ignoring null, that is, no null record will be obtained; indicating the total number of data in the statistical field that is not null
  • Get all records in sc table
SELECT COUNT(*) FROM sc
  • The number of records whose results are not null
SELECT COUNT(grade) FROM sc
  • Efficiency analysis
    Under the MyISAM storage engine, count( ) is highly efficient.
    Under the InnoDB storage engine, count(
    ) and count(grade) have similar efficiency.

Three, sum

  • Get the sum of a column, null will be ignored
  • When using MySQL's sum() function, if no data is obtained according to the query conditions, the result after sum() will be null instead of 0;
    if multiple pieces of data are obtained according to the query conditions, but the sum field in these data , Some have values, some are null, then sum() will also get the correct result. In short, in order to ensure correct and rigorous use, when using the sum() function in the future, you should use ifnull(sum(columnName), 0)
  • Get all the results and
SELECT SUM(grade) FROM sc

Four, avg

  • Take the average of a column
  • Can be combined with round() function to query the specified data percentage
  • Take average
SELECT AVG(grade) FROM sc
  • Query the percentage of scores greater than 70
SELECT ROUND(AVG(grade > 70),2) FROM sc

Five, max

  • Get the maximum value of a column
  • Query the maximum score
SELECT MAX(grade) FROM sc

Six, min

  • Get the minimum value of a column
  • Check minimum score
SELECT MIN(grade) FROM sc

Seven, combined use

SELECT COUNT(*),SUM(grade),AVG(grade),MAX(grade),MIN(grade) FROM sc

8. Group query

  • Mainly use group by and having
  • If there is a group by statement in the SQL statement, then the select statement can only be followed by the grouping function + the field participating in the grouping , which has no meaning in MySQL, but an error is reported in Oracle
  • If you want to filter the grouped data again, you need to use the having clause
  • Execution order of grouping functions:
    query data according to conditions.
    Grouping,
    using having filtering, to obtain correct data
  • Query the average grades grouped by student number
SELECT AVG(grade) FROM sc GROUP BY sno
  • Group by student number to query average scores greater than 70
SELECT AVG(grade) FROM sc GROUP BY sno HAVING AVG(grade) > 70

Quote, select statement summary

1. A complete select statement format:

select field
from table name
where …….
group by ………
having ……. (exists in order to filter the grouped data, that is, it cannot appear separately)
order by ………

2. Statement execution order

  1. First execute the where statement to filter the original data
  2. Perform group by to group
  3. Perform having operations on grouped data
  4. Execute select to select data
  5. Perform order by sort
  • Principle: The data that can be filtered in where, try to filter in where, the efficiency is higher. The filtering of having is to filter the data after the grouping

Guess you like

Origin blog.csdn.net/LvJzzZ/article/details/108991223