如何获得具有相同id的结果中mysql的排名
快速提问,我有一个消息表,每个消息都有时间戳、reply_id 和 topic_id。 Reply_ids 可以与其他消息相同,这意味着这些消息位于同一组回复中。当我选择带有最小时间戳的reply_id时,这意味着它是该组的第一条消息,我还想知道它在该topic_id的所有结果中的编号位置,例如。第三(3)第四(4)等有人知道如何做到这一点或有任何建议吗?这可以用纯sql来完成吗?
SELECT reply_id,min(timestamp) as min FROM messages
WHERE reply_chunk_id = ?
AND topic_id = ?
Quick question, I have a table of messages each with a timestamp and a reply_id and a topic_id. Reply_ids can be the same as others, which means those messages are in the same group of replies. When I select a reply_id with the min timestamp, meaning it was the first message of that group, I would also like to know its numbered place within all results of that topic_id, ex. 3rd (3) 4th(4) etc. Anyone know how to do this or have any suggestions? Can this be done in pure sql?
SELECT reply_id,min(timestamp) as min FROM messages
WHERE reply_chunk_id = ?
AND topic_id = ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用纯 SQL:
仅当
replyid
是唯一值时才有效。如果replyid
之前有重复的replyid
,则 COUNT 将会错过该内容。MySQL 不支持的分析函数将是更好的选择。您可以使用 MySQL SELECT 语句中的变量重新创建功能。
Using pure SQL:
This works only if
replyid
is unique values. If there's a duplicatereplyid
before thereplyid
, then the COUNT will miss that.Analytic functions, which MySQL doesn't support, would be a better option. You can recreate the functionality using variables in the MySQL SELECT statements.