如果项目已排序,我可以运行select语句并获取行号吗?
我有这样一张桌子:
mysql> describe orders;
+-------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------------------+------+-----+---------+----------------+
| orderID | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| itemID | bigint(20) unsigned | NO | | NULL | |
+-------------+---------------------+------+-----+---------+----------------+
Run Code Online (Sandbox Code Playgroud)
然后我可以运行此查询以按ID获取订单数量:
SELECT itemID, COUNT(*) as ordercount
FROM orders
GROUP BY itemID ORDER BY ordercount DESC;
Run Code Online (Sandbox Code Playgroud)
这给了我itemID
表中每个的计数如下:
+--------+------------+
| itemID | ordercount |
+--------+------------+
| 388 | 3 |
| 234 | …
Run Code Online (Sandbox Code Playgroud) 所以我有一张表如下:
ID_STUDENT | ID_CLASS | GRADE
-----------------------------
1 | 1 | 90
1 | 2 | 80
2 | 1 | 99
3 | 1 | 80
4 | 1 | 70
5 | 2 | 78
6 | 2 | 90
6 | 3 | 50
7 | 3 | 90
Run Code Online (Sandbox Code Playgroud)
我需要对它们进行分组,排序和排序以给出:
ID_STUDENT | ID_CLASS | GRADE | RANK
------------------------------------
2 | 1 | 99 | 1
1 | 1 | 90 | 2
3 | 1 | 80 …
Run Code Online (Sandbox Code Playgroud) 这个查询有什么问题?它在rowno列中显示为null.
SELECT @rowno:=@rowno+1 `rn`,`id`, `title`, `topic`
FROM stories,(SELECT @rownum:=0) r
WHERE newstype='2';
Run Code Online (Sandbox Code Playgroud)
我在'MySQL Query browser'中运行它
提前致谢.