MySQL result sorting - Aggregate functionsenvironmentCREATE TABLE `student` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'student number', `student_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'Student name', `sex` varchar(5) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT 'Gender\r\n', `age` int(11) NULL DEFAULT NULL COMMENT 'Age', `result` double(10, 0) NULL DEFAULT NULL COMMENT 'Results', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; INSERT INTO `student` VALUES (1, '小王', '男', 18, 90); INSERT INTO `student` VALUES (2, 'Xiao Li', 'Female', 19, 80); INSERT INTO `student` VALUES (3, 'Xiao Ming', 'Male', 20, 85); INSERT INTO `student` VALUES (4, '小张', '男', 21, 87); Query result sortingThe most common application is to filter popular products or hot searches on Weibo. Syntax format:
Field names 1 and 2 are the basis for sorting the query results. ASC means ascending order and DESC means descending order. The default is ASC. Raise your paw: SELECT * FROM student ORDER BY age DESC ; When followed by two sorting rules, the first field name is the same and the sorting is based on the second field name sorting rule. All you have to do is figure it out on your own. think: How to write the function to search for age in descending order and student_name in ascending order? Grouping and Aggregation of QueriesAggregate functions
Check the average age of studentsselect AVG(age) as "age" from student; Check the total number of peopleselect count(id) as "total number of people" from student; Find out how many people are of each age.select age, count(id) as "total number of people" from student GROUP BY age; Find out the maximum ageselect MAX(age) as "Maximum age" from student; Less than the same. Find out how many men and women there are select sex ,count(*) AS "Number of people" from student GROUP BY sex; #GROUP BY groups the results according to the following field names How much is the total score of the query? select sum(result) as "total score" FROM student; SummarizeThis article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Tomcat multi-instance deployment and configuration principles
>>: Initial summary of the beginner's website building tutorial
1. Windows Server 2019 Installation Install Windo...
Today, in the practice of vue3+vite project, when...
width: auto The child element (including content+...
1. Technical points involved vite version vue3 ts...
This post focuses on a super secret Flutter proje...
Table of contents Preface Related Materials Achie...
When using MySQL to query the database and execut...
Table of contents Integrity constraints Definitio...
There are three date types in MySQL: date(year-mo...
Table of contents 1. General steps for SQL optimi...
Generally speaking, when we view the contents of ...
Table of contents 1. Introduction to podman 2. Ad...
Writing a Dockerfile Configure yum source cd /tmp...
In order to solve the problem mentioned last time...
Generate a certificate chain Use the script to ge...