Mysql return 排序的行位置

Mysql return the row position with sorting

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)     +
+-----------------------------+

我想要一个 return 的“3”作为“113”行位置,其中按名称 Beta 分组并按 Asort

排序 ASC

您需要使用变量来计算排名 (row-number-in-mysql),因为 mySql 没有 ROW_NUMBER

SQL Fiddle Demo

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