Sorting and paging - "MySQL database"

Hello fellow CSDN users, today, the content of Xiaoyalan is sorting and paging in the MySQL database. Now, let us enter the world of sorting and paging! ! !


Sort data

Pagination


Sort data

Sorting rules

Sort using ORDER BY clause  

ASC (ascend): ascending order

DESC (descend): descending order

The ORDER BY clause is at the end of the SELECT statement.

Single column sort

SELECT   last_name, job_id, department_id, hire_date

FROM     employees

ORDER BY hire_date ;

 

SELECT   last_name, job_id, department_id, hire_date

FROM     employees

ORDER BY hire_date DESC ;

 

SELECT employee_id, last_name, salary*12 annsal

FROM   employees

ORDER BY annsal;

 

Sort by multiple columns

SELECT last_name, department_id, salary

FROM   employees

ORDER BY department_id, salary DESC;

 

It is possible to sort using columns that are not in the SELECT list.

When sorting multiple columns, the first column that is sorted must have the same column value before the second column is sorted. If all values ​​in the first column of data are unique, the second column will no longer be sorted.  


Pagination

background

Background 1: There are too many records returned by the query, which is very inconvenient to view. How can we implement paging query?

Background 2: There are 4 pieces of data in the table. What should we do if we only want to display the 2nd and 3rd pieces of data?

Implementation rules

  • Paging principle - The so-called paging display is the conditions required to display the result set in the database section by section.
  • Using LIMIT to implement paging in MySQL
  • Format: LIMIT [position offset,] number of lines
  • The first "position offset" parameter indicates which row MySQL starts displaying. It is an optional parameter. If the "position offset" is not specified, it will start from the first record in the table (the first record The position offset of the second record is 0, the position offset of the second record is 1, and so on); the second parameter "number of rows" indicates the number of records returned.
  • Example

--The first 10 records:

SELECT * FROM table name LIMIT 0,10;

or

SELECT * FROM table name LIMIT 10;

--Records 11 to 20:

SELECT * FROM table name LIMIT 10,10;

--Records 21 to 30:

SELECT * FROM table name LIMIT 20,10;

 "LIMIT 3 OFFSET 4" can be used in MySQL 8.0, which means to get the next 3 records starting from the 5th record, which is the same as the result returned by "LIMIT 4,3;".

Pagination explicit formula: (current page number - 1) * number of items per page, number of items per page

SELECT * FROM table 

LIMIT(PageNo - 1)*PageSize,PageSize;

Note: The LIMIT clause must be placed at the end of the entire SELECT statement!

Benefits of using LIMIT: Constraining the number of returned results can reduce the amount of network transmission of the data table and improve query efficiency. If we know that there is only 1 result returned, we can use LIMIT 1 to tell the SELECT statement to only return one record. The advantage of this is that SELECT does not need to scan the entire table, it only needs to retrieve a record that meets the conditions and return it.

expand

The keywords used may differ in different DBMS. The LIMIT keyword is used in MySQL, PostgreSQL, MariaDB and SQLite, and needs to be placed at the end of the SELECT statement.

If it is SQL Server and Access, you need to use the TOP keyword, such as:

SELECT TOP 5 name, hp_max FROM heros ORDER BY hp_max DESC

If it is DB2, use keywords like FETCH FIRST 5 ROWS ONLY:

SELECT name, hp_max FROM heros ORDER BY hp_max DESC FETCH FIRST 5 ROWS ONLY

If it is Oracle, you need to count the number of rows based on ROWNUM:

 SELECT rownum,last_name,salary FROM employees WHERE rownum < 5 ORDER BY salary DESC;

It should be noted that this statement first retrieves the first 5 data rows, and then sorts them from high to low according to hp_max. But the results produced by this are different from those of the above method. 


Okay, that’s all Xiao Yalan’s content for today, keep up the good work! ! !

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/132917272