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

Detailed explanation of JS ES6 variable destructuring assignment

Table of contents 1. What is deconstruction? 2. A...

HTML line spacing setting methods and problems

To set the line spacing of <p></p>, us...

Solution to the img tag problem below IE10

Find the problem I wrote a simple demo before, bu...

Free tool to verify that HTML, CSS and RSS feeds are correct

One trick for dealing with this type of error is t...

Baidu Input Method opens API, claims it can be ported and used at will

The relevant person in charge of Baidu Input Metho...

CSS Summary Notes: Examples of Transformations, Transitions, and Animations

1. Transition Transition property usage: transiti...

XHTML Getting Started Tutorial: XHTML Hyperlinks

It is no exaggeration to say that hyperlinks conne...

Use h1, h2, and h3 tags appropriately

In the process of making web pages, it is inevita...

Analysis of different MySQL table sorting rules error

The following error is reported when MySQL joins ...

Docker exposes port 2375, causing server attacks and solutions

I believe that students who have learned about th...

Docker solves the problem that the terminal cannot input Chinese

Preface: One day, I built a MySQL service in Dock...

Pure CSS to adjust Div height according to adaptive width (percentage)

Under the requirements of today's responsive ...

Install Apple Mac OS X in VMWare12 Graphic Tutorial

1. Introduction: Because my friend wanted to lear...