gpt4 book ai didi

mysql - 根据id的mysql对行进行排序

转载 作者:行者123 更新时间:2023-11-29 00:39:06 26 4
gpt4 key购买 nike

我有以下查询。

SET @rownum := 0;
SELECT result.rank, result.postid FROM (
SELECT CASE @rownum

WHEN 5 THEN @rownum:=1 ELSE @rownum:=@rownum + 1 END AS rank
,c.postid

FROM comments c
ORDER BY c.postid DESC
) as result

它以下列形式返回结果。

      rank               postid
---------------------------------
1 199
2 199
3 199
4 199
5 198
1 198
2 198
3 198
4 198
5 198
6 198

现在我想更新此查询,以便根据 postid 指定排名。postid 199 将从 1 排名到 4,一旦它识别出新的 postid 出现,它应该再次将其从 1 排名到任何数字id 包含的记录数。

我希望每个 postid 的排名计数从 1 开始,然后转到该 postid 出现的次数。

编辑:

@lieven 检查这张图片还是有问题。

如您所见,帖子 172 完成但排名仍在继续

最佳答案

您可以按多个字段排序:

ORDER BY c.postid DESC, c.rank

第一个具有最高优先级。

关于mysql - 根据id的mysql对行进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063864/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com