Sql 日期选择彼此 X 秒范围内的行
我的 sql 表类似于 (message,created)
我想选择彼此相距在 X 秒内的那些行。 假设最后一条消息是在 NOW() 之后的 X 秒内,那么它应该选择它。 如果倒数第二条消息距离上一条消息在 X 秒内,那么它也应该选择它。 换句话说,每一行都应该与下一行进行比较并检查。 对于最后一行,应该用 NOW() 检查。 基本上我想要最后一个消息会话(即我们相互链接的最后一组消息,假设 X 秒内的后续消息相互链接),
我不知道如何为此编写 SQL 查询。 有可能吗?
非常感谢您的宝贵时间。
My sql table is something like (message,created)
I want to select those rows which are within X seconds from each other. Say the last message is within X seconds from NOW(), then it should select it. If the second last message is within X seconds from the last message then it should select it too. In other words each row should be compared with the next row and checked. For the last row it should be check with say NOW(). Basically I want the last session of messages (i.e. last set of messages that we linked to each other, assuming that consequtive messages within X seconds are linked to each other)
I have no idea how to go about writing an SQL query for this. Is it even possible?
Thank you very much for your time.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该脚本在 SQLServer 中运行。 您应该能够取出 select 语句并在 MySQL 中运行它。
This script works in SQLServer. You should be able to take out the select statement and run it in MySQL.
我相信你想得太复杂了(但话又说回来,也许我误解了要求?)
这会选择特定消息之前 30 秒内创建的所有消息:
其中 17 当然是你感兴趣的消息 Id,30 是数字您的时间范围内的秒数。
I believe you are thinking too complicated (But then again, maybe I misunderstood the requirement?)
This selects all messages created within 30 seconds before a particular message:
where 17 is of course the message Id you are interested in, and 30 is the number of seconds in your time frame.