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
First: 4 ways to introduce CSS There are four way...
Preface The language classification of SQL mainly...
Recently I wrote in my blog that in the project l...
First execute the command: [root@mini61 setuptool...
Problems: After adding the -v parameter to docker...
Create a user: create user 'oukele'@'...
Preface: Partitioning is a table design pattern. ...
getElementById cannot get the object There is a s...
CocosCreator version 2.3.4 Dragon bone animation ...
As shown below: select a1,a2,a1+a2 a,a1*a2 b,a1*1...
When you write buttons (input, button), you will f...
Ubuntu is a relatively popular Linux desktop syst...
The JD carousel was implemented using pure HTML a...
Table of contents Initialize computed Dependency ...
introduction You must have encountered this in an...