MYSQL basic learning - MySQL sorting and paging query

foreword

Starting today, this series of content will take your friends to learn database technology. Database technology is an indispensable part of knowledge content in Java development. It is also a very important technology. This series of tutorials comprehensively explains the database system from the shallower to the deeper. It is very suitable for small partners with zero foundation to learn.
insert image description here


The full text is about [1045] words , no nonsense, just pure dry stuff that allows you to learn techniques and understand principles! This article has a wealth of cases and pictures, so that you can better understand and use the technical concepts in the article, and can bring you enough enlightening thinking...

1. Sort query

What is a sort query?

Sorting is to arrange the query results in ascending or descending order according to a certain field, followed by ASC in ascending order, followed by DESC keyword in descending order. If no ASC or DESC keywords are added, the default is ascending order.

语法:SELECT 列名 FROM 表名 WHERE 条件 ORDER BY 列名 ASC|DESC

Take a chestnut:

# 按照年龄升序排序
SELECT SId,Sname,Sage,Ssex FROM student ORDER BY Sage

# 按照年龄降序排序
SELECT SId,Sname,Sage,Ssex FROM student ORDER BY Sage DESC

2. Paging query

What is paging query? Don't worry, Brother Jian will answer it right away. If a table has a lot of data, and the query results have too much data for users to read, pagination can be used to solve the problem. You can only query 10, 20 or a specific amount of data per page, and then query the next 10 or 20 when the user clicks on the next page. This form of display is called pagination.

语法:SELECT 列名 FROM 表名 WHERE 条件 LIMIT 起始条,每页查询条数

# 不加分页查询学生的所有信息
SELECT SId,Sname,Sage,Ssex FROM student 

There are a total of 16 pieces of data without pagination above. In the real situation of an enterprise, the amount of data may be hundreds of thousands, millions or even more. Let's add the pagination syntax to try it out.

# 查询第一页数据,从第0条开始查询,每页查询5条
SELECT SId,Sname,Sage,Ssex FROM student LIMIT 0,5;

# 查询第二页数据,从第6条开始查询,每页查询5条
SELECT SId,Sname,Sage,Ssex FROM student LIMIT 6,5;


3. Conclusion

Finally, here is a summary of the core points of this article:

  1. Sorting query and paging query are relatively basic and simple SQL syntax, which are frequently used, so you need to be familiar with them.

  2. Proficiency in using sorting knows how to sort in ascending and descending order, and know that the default order is ascending order without adding ASC.

  3. Pagination query has two parameters. The first is the number of initial query items. By default, MySQL counts from the 0th item. The second parameter is the number of query items per page. For example, if you want to display 10 items of data per page, then For the second parameter, write 10 and so on.

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/130411854