Sql 日期选择彼此 X 秒范围内的行

发布于 2024-07-19 08:16:11 字数 295 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

亢潮 2024-07-26 08:16:11

该脚本在 SQLServer 中运行。 您应该能够取出 select 语句并在 MySQL 中运行它。

DECLARE @Messages TABLE (Message VARCHAR(10), Created DATETIME)
DECLARE @Interval FLOAT

-- Interval is 1 day.
SET @Interval = 1

-- These should be in result
INSERT INTO @Messages VALUES ('Message1', GetDate())    
INSERT INTO @Messages VALUES ('Message2', GetDate()-1)
-- These should not be in result
INSERT INTO @Messages VALUES ('Message3', GetDate()-3)
INSERT INTO @Messages VALUES ('Message4', GetDate()-5)

SELECT m1.Message, m1.Created
FROM @Messages m1
     INNER JOIN @Messages m2 ON m2.Created <= m1.Created + @Interval                                
                                AND m2.Created >= m1.Created
                                AND m2.Message <> m1.Message
UNION ALL SELECT m2.Message, m2.Created
FROM @Messages m1
     INNER JOIN @Messages m2 ON m2.Created <= m1.Created + @Interval                                
                                AND m2.Created >= m1.Created
                                AND m2.Message <> m1.Message
ORDER BY Created

This script works in SQLServer. You should be able to take out the select statement and run it in MySQL.

DECLARE @Messages TABLE (Message VARCHAR(10), Created DATETIME)
DECLARE @Interval FLOAT

-- Interval is 1 day.
SET @Interval = 1

-- These should be in result
INSERT INTO @Messages VALUES ('Message1', GetDate())    
INSERT INTO @Messages VALUES ('Message2', GetDate()-1)
-- These should not be in result
INSERT INTO @Messages VALUES ('Message3', GetDate()-3)
INSERT INTO @Messages VALUES ('Message4', GetDate()-5)

SELECT m1.Message, m1.Created
FROM @Messages m1
     INNER JOIN @Messages m2 ON m2.Created <= m1.Created + @Interval                                
                                AND m2.Created >= m1.Created
                                AND m2.Message <> m1.Message
UNION ALL SELECT m2.Message, m2.Created
FROM @Messages m1
     INNER JOIN @Messages m2 ON m2.Created <= m1.Created + @Interval                                
                                AND m2.Created >= m1.Created
                                AND m2.Message <> m1.Message
ORDER BY Created
七颜 2024-07-26 08:16:11

我相信你想得太复杂了(但话又说回来,也许我误解了要求?)

这会选择特定消息之前 30 秒内创建的所有消息:

SELECT
  Id,
  MessageText,
  MessageDate
FROM
  Message
WHERE
  TIME_TO_SEC(TIMEDIFF(
    (
      SELECT MessageDate 
      FROM   Message
      WHERE  Id = 17
    ),
    MessageDate
  )) <= 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:

SELECT
  Id,
  MessageText,
  MessageDate
FROM
  Message
WHERE
  TIME_TO_SEC(TIMEDIFF(
    (
      SELECT MessageDate 
      FROM   Message
      WHERE  Id = 17
    ),
    MessageDate
  )) <= 30

where 17 is of course the message Id you are interested in, and 30 is the number of seconds in your time frame.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文