MySQL uses aggregate functions to query a single table

MySQL uses aggregate functions to query a single table

Aggregate functions

Acts on a set of data and returns a value for that set of data

  • count: the number of records in the statistical result.
  • max: maximum value of statistics
  • min: minimum statistical value
  • sum: calculate the sum
  • avg: Calculate the average value

Note the grouping function group by. If you want to filter the grouped data, you must use the having keyword, and the condition should be written after having;

  • where: filter the existing data first, then group it, and then calculate the aggregate function;
  • having: Filter the data after grouping. Insert the city table we need to practice
CREATE TABLE `city` (
	`id` INT AUTO_INCREMENT PRIMARY KEY COMMENT 'Primary key',
	`city_name` VARCHAR(100) COMMENT 'city name',
	`city_year` VARCHAR(4) COMMENT 'Year, yyyy',
	`city_gdp` DOUBLE COMMENT 'The GDP of the city for the current year, in 100 million yuan',
	`city_population` DOUBLE COMMENT 'The total population of the city in the current year of good harvest, in 10,000 people'
);

Add data

INSERT INTO city VALUES(1,'Shanghai',2018,32679,2418);
INSERT INTO city VALUES(2,'Beijing',2018,30320,2171);
INSERT INTO city VALUES(3,'深圳',2018,24691,1253);
INSERT INTO city VALUES(4,'Guangzhou',2018,23000,1450);
INSERT INTO city VALUES(5,'重庆',2018,20363,3372);
INSERT INTO city VALUES(6,'Shanghai',2019,38155,2424);
INSERT INTO city VALUES(7,'Beijing',2019,35371,2171);
INSERT INTO city VALUES(8,'深圳',2019,26927,1302);
INSERT INTO city VALUES(9,'Guangzhou',2019,23628,1491);
INSERT INTO city VALUES(10,'重庆',2019,23605,3372);

COUNT

Query and calculate how many cities have entered data in 2019 (answers 5)

SELECT COUNT(*) FROM city WHERE city_year = '2019';

insert image description here

MAX

Check what the highest GDP was in 2018;

SELECT MAX(city_gdp) FROM city WHERE city_year = '2018';

insert image description here

MIN

Find out what the lowest GDP was in 2018;

SELECT MIN(city_gdp) FROM city WHERE city_year = '2018';

insert image description here

SUM

Query the total GDP of all cities in 2019;

SELECT SUM(city_gdp) FROM city WHERE city_year = '2019';

insert image description here

AVG

Query the average GDP of all cities in 2019;

SELECT AVG(city_gdp) FROM city WHERE city_year = '2019';

insert image description here

For other MySQL articles, please see the following links

MySQL DDL statements

MySQL CRUD statements

MySQL Aggregate Functions

MySQL multi-table query

END…

This is the end of this article about using aggregate functions in MySQL to query a single table. For more information about using aggregate functions in MySQL to query a single table, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL grouping queries and aggregate functions
  • Mysql aggregate function nested use operation
  • Analysis of MySQL query sorting and query aggregation function usage
  • How to add conditional expressions to aggregate functions in MySql
  • MySQL aggregate function sorting

<<:  Insufficient memory problem and solution when docker starts elasticsearch

>>:  Tomcat Nginx Redis session sharing process diagram

Recommend

js implements the pop-up login box by clicking the pop-up window

This article shares the specific code of js to re...

Summary of tips for setting the maximum number of connections in MySQL

Method 1: Command line modification We only need ...

Let's learn about the MySQL storage engine

Table of contents Preface 1. MySQL main storage e...

Summary on Positioning in CSS

There are four types of positioning in CSS, which...

Abbreviation of HTML DOCTYPE

If your DOCTYPE is as follows: Copy code The code ...

The vue project realizes drawing a watermark in a certain area

This article shares with you how to use Vue to dr...

Detailed steps to configure MySQL remote connection under Alibaba Cloud

Preface As we all know, by default, the MySQL ins...

Implementation of services in docker accessing host services

Table of contents 1. Scenario 2. Solution 3. Conc...

Problems with using wangeditor rich text editing in Vue

wangEditor is a web rich text editor developed ba...

Implementation of CSS3 button border animation

First look at the effect: html <a href="#...

Detailed explanation of Nginx status monitoring and log analysis

1. Nginx status monitoring Nginx provides a built...

A brief discussion on whether MySQL can have a function similar to Oracle's nvl

Use ifnull instead of isnull isnull is used to de...

How CSS affects the white screen time during initial loading

Rendering pipeline with external css files In the...