简体   繁体   中英

Mysql return the row position with sorting

the ff table:

+-----------------------------+
+ value| name   |Asort        +
+-----------------------------+
+  111 | Alpha  |a            +
+  161 | Beta   |b            +
+  092 | Delta  |c            +
+  141 | Beta   |a            +
+  113 | Beta   |e            +
+  092 | Delta  |f            +
+  ...                        +
+  ... | more items           +
+-----------------------------+




+-----------------------------+
+ value| name |Asort          +
+-----------------------------+
+  141 | Beta |a              +
+  161 | Beta |b              +
+  113 | Beta |c              +
+  ...                        +
+  ... | more items(Beta)     +
+-----------------------------+

i want a return of "3" as "113" row position that where group by name Beta and sort ASC by Asort

You need use a variable to calculate rank ( row-number-in-mysql ) because mySql doesnt have ROW_NUMBER .

SQL Fiddle Demo

SELECT ff.*, 
       @rownum := @rownum + 1 AS rank
  FROM ff, 
       (SELECT @rownum := 0) r
WHERE name = 'Beta'  
ORDER BY Asort

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM