Aggregate functions Acts on a set of data and returns a value for that set of data
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;
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'; MAX Check what the highest GDP was in 2018; SELECT MAX(city_gdp) FROM city WHERE city_year = '2018'; MIN Find out what the lowest GDP was in 2018; SELECT MIN(city_gdp) FROM city WHERE city_year = '2018'; SUM Query the total GDP of all cities in 2019; SELECT SUM(city_gdp) FROM city WHERE city_year = '2019'; AVG Query the average GDP of all cities in 2019; SELECT AVG(city_gdp) FROM city WHERE city_year = '2019'; 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:
|
<<: Insufficient memory problem and solution when docker starts elasticsearch
>>: Tomcat Nginx Redis session sharing process diagram
Sometimes you need to access some static resource...
Assume there are two Linux servers A and B, and w...
Click here to return to the 123WORDPRESS.COM HTML ...
Basic syntax You can create a view using the CREA...
1. Preparation 1.1 Download the tomcat compressed...
MySQL Query Cache is on by default. To some exten...
1. Document flow and floating 1. What is document...
Table of contents 1. Install axios 2. Use of axio...
Table of contents Preface 1. Split a string 2. JS...
Preface Today, a developer gave me feedback that ...
1. The first method is to start the local tomcat ...
Let's briefly sort out the configuration of s...
Only display Docker container mount directory inf...
The <link> tag defines the relationship bet...
What is MIME TYPE? 1. First, we need to understan...