Home Database Mysql Tutorial MySQL实现Oracle或者PostgreSQL的row_numberover这样的排名语法_MySQL

MySQL实现Oracle或者PostgreSQL的row_numberover这样的排名语法_MySQL

Jun 01, 2016 pm 01:26 PM
oracle

bitsCN.com PostgreSQL 和Oracle 都提供了 row_number() over() 这样的语句来进行对应的字段排名, 很是方便。 MySQL却没有提供这样的语法。
Table "ytt.t1" Column | Type | Modifiers --------+-----------------------+----------- i_name | character varying(10) | not null rank | integer | not null

t_girl=# select * from t1 order by i_name; i_name | rank ---------+------ Charlie | 12 Charlie | 12 Charlie | 13 Charlie | 10 Charlie | 11 Lily | 6 Lily | 7 Lily | 7 Lily | 6 Lily | 5 Lily | 7 Lily | 4 Lucy | 1 Lucy | 2 Lucy | 2 Ytt | 14 Ytt | 15 Ytt | 14 Ytt | 14 Ytt | 15 (20 rows)
Copy after login
t_girl=# select i_name,rank, row_number() over(partition by i_name order by rank desc) as rank_number from t1; i_name | rank | rank_number ---------+------+------------- Charlie | 13 | 1 Charlie | 12 | 2 Charlie | 12 | 3 Charlie | 11 | 4 Charlie | 10 | 5 Lily | 7 | 1 Lily | 7 | 2 Lily | 7 | 3 Lily | 6 | 4 Lily | 6 | 5 Lily | 5 | 6 Lily | 4 | 7 Lucy | 2 | 1 Lucy | 2 | 2 Lucy | 1 | 3 Ytt | 15 | 1 Ytt | 15 | 2 Ytt | 14 | 3 Ytt | 14 | 4 Ytt | 14 | 5 (20 rows)
Copy after login
Copy after login


 

t_girl=# select i_name,rank, row_number() over(partition by i_name ) as rank_number from t1; i_name  | rank | rank_number ---------+------+------------- Charlie	 |   12 |           1 Charlie	|   12 |           2 Charlie	|   13 |           3 Charlie	|   10 |           4 Charlie	|   11 |           5 Lily		|    6 |           1 Lily		|    7 |           2 Lily		|    7 |           3 Lily		|    6 |           4 Lily		|    5 |           5 Lily		|    7 |           6 Lily		|    4 |           7 Lucy	|    1 |           1 Lucy	|    2 |           2 Lucy	|    2 |           3 Ytt		|   14 |           1 Ytt		|   15 |           2 Ytt		|   14 |           3 Ytt		|   14 |           4 Ytt		|   15 |           5(20 rows)
Copy after login



t_girl=# select i_name,rank, row_number() over() as rank_number from t1; i_name  | rank | rank_number ---------+------+------------- Lily		 |    7 |           1 Lucy	|    2 |           2 Ytt		|   14 |           3 Ytt		|   14 |           4 Charlie	|   12 |           5 Charlie	|   13 |           6 Lily		|    7 |           7 Lily		|    4 |           8 Ytt		|   14 |           9 Lily		|    6 |          10 Lucy	|    1 |          11 Lily		|    7 |          12 Ytt		|   15 |          13 Lily		|    6 |          14 Charlie	|   11 |          15 Charlie	|   12 |          16 Lucy	|    2 |          17 Charlie	|   10 |          18 Lily		|    5 |          19 Ytt		|   15 |          20(20 rows)
Copy after login


DELIMITER $$ USE `t_girl`$$ DROP PROCEDURE IF EXISTS `sp_rownumber`$$ CREATE PROCEDURE `sp_rownumber`( IN f_table_name VARCHAR(64), IN f_column_partitionby VARCHAR(64), IN f_column_orderby VARCHAR(64), IN f_is_asc CHAR(4) ) BEGIN -- Created by ytt at 2014/1/10 -- Do a row_number() over() DECLARE i INT; -- Create a temporary table to save result. DROP TABLE IF EXISTS tmp_rownum; SET @stmt = CONCAT('create temporary table tmp_rownum select *,''rownum'' from ',f_table_name,' where 1 = 0'); PREPARE s1 FROM @stmt; EXECUTE s1; SET i = 0; SET @j = 0; SET @v_column_paritionby = ''; -- Check whether parition column is null or not. IF (f_column_partitionby = '' OR f_column_partitionby IS NULL) THEN -- No additional parition column. SET @stmt = CONCAT('insert into tmp_rownum select *,@j:= @j+1 as rownum from ', f_table_name); PREPARE s1 FROM @stmt; EXECUTE s1; ELSE -- Give partition column. SET @stmt = CONCAT('select count(*) from (select count(*) from ',f_table_name,' group by ', f_column_partitionby,') as a into @cnt'); PREPARE s1 FROM @stmt; EXECUTE s1; WHILE i < @cnt DO -- Get the partition value one by one. SET @stmt = CONCAT('select ',f_column_partitionby,' from ',f_table_name,' group by ',f_column_partitionby,' limit ',i,',1 into @v_column_partitionby'); PREPARE s1 FROM @stmt; EXECUTE s1; -- Check whether sort is needed. IF f_column_orderby = '' OR f_column_orderby IS NULL THEN SET @stmt = CONCAT('insert into tmp_rownum select *,@j:= @j+1 as rownum from ', f_table_name,' where ',f_column_partitionby,' = ''',@v_column_partitionby,''''); ELSE SET @stmt = CONCAT('insert into tmp_rownum select *,@j:= @j+1 as rownum from ', f_table_name,' where ',f_column_partitionby,' = ''',@v_column_partitionby,''' order by ',f_column_orderby,' ',f_is_asc); END IF; SET @j = 0; PREPARE s1 FROM @stmt; EXECUTE s1; SET i = i + 1; END WHILE; END IF; -- Reset all session variables. SET @j = NULL; SET @v_column_paritionby = NULL; SET @cnt = NULL; SELECT * FROM tmp_rownum; END$$ DELIMITER ;
Copy after login
Copy after login


CALL sp_rownumber(&#39;t1&#39;,&#39;i_name&#39;,&#39;rank&#39;,&#39;desc&#39;);query resulti_name	rank	rownumCharlie	13	1Charlie	12	2Charlie	12	3Charlie	11	4Charlie	10	5Lily		7	1Lily		7	2Lily		7	3Lily		6	4Lily		6	5Lily		5	6Lily		4	7Lucy	2	1Lucy	2	2Lucy	1	3Ytt		15	1Ytt		15	2Ytt		14	3Ytt		14	4Ytt		14	5
Copy after login

第二种,

CALL sp_rownumber(&#39;t1&#39;,&#39;i_name&#39;,NULL,NULL);query resulti_name	rank	rownumCharlie	12	1Charlie	13	2Charlie	11	3Charlie	12	4Charlie	10	5Lily		7	1Lily		7	2Lily		4	3Lily		6	4Lily		7	5Lily		6	6Lily		5	7Lucy	2	1Lucy	1	2Lucy	2	3Ytt		14	1Ytt		14	2Ytt		14	3Ytt		15	4Ytt		15	5
Copy after login

第三种,

CALL sp_rownumber(&#39;t1&#39;,NULL,NULL,NULL);query resulti_name	rank	rownumLily		7	1Lucy	2	2Ytt		14	3Ytt		14	4Charlie	12	5Charlie	13	6Lily		7	7Lily		4	8Ytt		14	9Lily		6	10Lucy	1	11Lily		7	12Ytt		15	13Lily		6	14Charlie	11	15Charlie	12	16Lucy	2	17Charlie	10	18Lily		5	19Ytt		15	20
Copy after login

 


 

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24
What to do if the oracle can't be opened What to do if the oracle can't be opened Apr 11, 2025 pm 10:06 PM

Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

How to solve the problem of closing oracle cursor How to solve the problem of closing oracle cursor Apr 11, 2025 pm 10:18 PM

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

How to create cursors in oracle loop How to create cursors in oracle loop Apr 12, 2025 am 06:18 AM

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

What to do if the oracle log is full What to do if the oracle log is full Apr 12, 2025 am 06:09 AM

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Oracle's Role in the Business World Oracle's Role in the Business World Apr 23, 2025 am 12:01 AM

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

How to stop oracle database How to stop oracle database Apr 12, 2025 am 06:12 AM

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.

How to export oracle view How to export oracle view Apr 12, 2025 am 06:15 AM

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

See all articles