1. ROW_NUMBER() Definition: The ROW_NUMBER() function is used to sort the data queried by select, adding a serial number to each piece of data. It cannot be used to rank student scores, and is generally used for paging queries. Examples: 1.1 Sorting students’ grades Here number is the serial number of each student, which is desc in reverse order according to studentScore (score) 1.2 Get the second student's score information The idea used here is the idea of paging query. Add another layer 2. RANK() Definition: RANK() function, as the name suggests, is a ranking function that can rank a certain field. Why is it different from ROW_NUMBER()? ROW_NUMBER() is for sorting. When there are students with the same scores, ROW_NUMBER() will sort them in sequence, and their serial numbers are different, while Rank() will not be the same, and their rankings are the same. Let’s look at the example below: 2.1 Ranking students’ grades Here you can see how ROW_NUMBER() and RANK() are the same? Because the students' scores are different, the ranking and sorting are the same. You will see the difference if you change it below. When two students have the same score, changes occur. RANK() is 1 2 2, while ROW_NUMBER() is still 1 2 3. This is the difference between RANK() and ROW_NUMBER(). 3.DENSE_RANK() Definition: The DENSE_RANK() function is also a ranking function. It is similar to RANK() in that it also ranks fields. So what is the difference between it and RANK()? See the example: Examples: The difference between DENSE_RANK() and RANK() is the continuity of ranking. DENSE_RANK() is continuous, while RANK() is a jumping ranking, so the ranking function used in general is RANK(). 4.NTILE() Definition: The NTILE() function distributes the rows in an ordered partition into a specified number of groups. Each group is numbered, starting from 1, just like what we call a 'partition', divided into several zones, and how many zones there are in a zone. Examples: The query was performed three times here. The first time it was divided into one 'zone', so the query result numbers were all 1. The second time it was divided into two zones, the query result was 1 1 2, which means that the first 'zone' has two numbered data, 1 1, and the second 'zone' has only the data 2. At this point, we have finished discussing the SQL ranking issue. Next time, we will introduce some in-depth SQL ranking statements. This is the end of this article about the use of the four major sql ranking functions ROW_NUMBER, RANK, DENSE_RANK, and NTILE. For more information about the use of the sql ranking functions ROW_NUMBER, RANK, DENSE_RANK, and NTILE, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Ubuntu installation cuda10.1 driver implementation steps
>>: Vue implements adding watermark to uploaded pictures
If you want to solve the slow problem once and fo...
constraint Constraints ensure data integrity and ...
Copy code The code is as follows: <!DOCTYPE ht...
MySQL filtering timing of where conditions and ha...
FileReader is an important API for front-end file...
The previous article explained how to reset the M...
Three ways to introduce CSS 1. Inline styles Adva...
Bugs As shown in the figure, I started to copy th...
mysql installer community 8.0.16.0 installation g...
Basic Environment Pagoda installation service [Py...
With the rise of mobile terminals such as iPad, p...
Table of contents 1. Sample code 2. See the essen...
When displaying long data in HTML, you can cut off...
To achieve the following goals: Mysql database wi...
This article shares the specific code of js to re...