Analysis of MySQL query sorting and query aggregation function usage

Analysis of MySQL query sorting and query aggregation function usage

This article uses examples to illustrate the use of MySQL query sorting and query aggregate functions. Share with you for your reference, the details are as follows:

Sorting

To facilitate viewing of data, you can sort the data

grammar:

select * from table name order by column 1 asc|desc [, column 2 asc|desc,...]

illustrate

Sort the row data by column 1. If the values ​​of some rows and column 1 are the same, sort them by column 2, and so on.

  • By default, the columns are sorted from small to large (asc)
  • asc Arrange from small to large, that is, ascending order
  • desc Sort from large to small, that is, descending order

Example 1: Query the information of undeleted male students in descending order by student ID

select * from students where gender=1 and is_delete=0 order by id desc;

Example 2: Query the information of undeleted students in ascending order by name

select * from students where is_delete=0 order by name;

Example 3: Display all student information, sort by age from oldest to youngest, and then sort by height from tallest to shortest when the ages are the same

select * from students order by age desc,height desc;

Aggregate functions

In order to quickly obtain statistical data, the following five aggregation functions are often used:

total

count(*) means to calculate the total number of rows. The result is the same if you write a star and a column name in brackets.

Example 1: Query the total number of students

select count(*) from students;

Maximum

max(column) means to find the maximum value of this column

Example 2: Query the maximum number of girls

select max(id) from students where gender=2;

Minimum

min(column) means to find the minimum value of this column

Example 3: Query the minimum number of students who have not been deleted

select min(id) from students where is_delete=0;

Sum

sum(column) means to find the sum of this column

Example 4: Query the total age of boys

select sum(age) from students where gender=1;
-- Average age select sum(age)/count(*) from students where gender=1;

average value

avg(column) means to find the average value of this column

Example 5: Query the average number of girls who have not been deleted

select avg(id) from students where is_delete=0 and gender=2;

Readers who are interested in more MySQL-related content can check out the following topics on this site: "MySQL query skills", "MySQL common functions summary", "MySQL log operation skills", "MySQL transaction operation skills summary", "MySQL stored procedure skills" and "MySQL database lock related skills summary"

I hope this article will be helpful to everyone's MySQL database design.

You may also be interested in:
  • MySQL aggregate function sorting
  • MySQL grouping queries and aggregate functions
  • Usage and performance optimization techniques of aggregate function count in MySQL
  • How to add conditional expressions to aggregate functions in MySql
  • MySQL essential basics: grouping function, aggregate function, grouping query detailed explanation

<<:  Vue implements login verification code

>>:  Implementation of Nginx load balancing cluster

Recommend

Question about custom attributes of html tags

In previous development, we used the default attr...

NestJs uses Mongoose to operate MongoDB

I recently started learning the NestJs framework....

Sample code for implementing 3D book effect with CSS

Without further ado, let's take a look at the...

Tomcat8 uses cronolog to split Catalina.Out logs

background If the catalina.out log file generated...

VMware vCenter 6.7 installation process (graphic tutorial)

background I originally wanted to download a 6.7 ...

Implementation of Nginx+ModSecurity security module deployment

Table of contents 1. Download 2. Deployment 1.Ngi...

Overview and Introduction to Linux Operating System

Table of contents 1. What is an Operating System ...

A brief discussion on HTML doctype and encoding

DOCTYPE Doctype is used to tell the browser which...

Flex layout allows subitems to maintain their own height

When using Flex layout, you will find that when a...

Solution to CSS anchor positioning being blocked by the top fixed navigation bar

Many websites have a navigation bar fixed at the ...

An example of how JavaScript can prevent duplicate network requests

Preface During development, we often encounter va...