简体   繁体   English

MySQL 5.7 查询以根据发送和响应日期擦除消息内容

[英]MySQL 5.7 Query to erase message content based on sent and response date

I'm having issues writing a query to erase the content of "inactive messages".我在编写查询以删除“非活动消息”的内容时遇到问题。 Erasing the content is just replacing the content of the message with "erased".擦除内容只是将消息内容替换为“已擦除”。

A message is considered inactive when:在以下情况下,消息被视为非活动状态:

  1. The sent date < NOW - 720 days发送日期 < NOW - 720 天
  2. AND the message response date < NOW - 720 days并且消息回复日期 < NOW - 720 天

This would be easy if I had the message response date, but I don't.如果我有消息回复日期,这会很容易,但我没有。 The only thing I have, is the "reply to message ID".我唯一拥有的是“回复消息 ID”。 The response date on a message would be the sent date of the LAST response message.消息的响应日期将是 LAST 响应消息的发送日期。

If message with ID 3 is a reply to message with ID 2 which was a reply to message with ID 1, the response date of message with ID 1 would be the sent date of message with ID 3.如果 ID 3 的消息是对 ID 2 消息的回复,而 ID 是对 ID 1 消息的回复,则 ID 1 消息的响应日期将是 ID 3 消息的发送日期。

Here is the table:这是表: 桌子

I tried this query to get all the "last" replies to messages but I'm not sure this will help me.我尝试使用此查询来获取对消息的所有“最后”回复,但我不确定这对我有帮助。

SELECT id, replyToMessage_id, sentDate
from Message OuterReference
where not exists (select 1 from Message where replyToMessage_id = OuterReference.ID)

Thanks in advance for your help!在此先感谢您的帮助!

Check this检查这个

DELETE t1
FROM Message t1
LEFT JOIN ( SELECT replyToMessage_id, MAX(sentDate) MaxSentDate
            FROM Message t2
            GROUP BY replyToMessage_id ) t3 ON t3.replyToMessage_id IN (t1.id, t1.replyToMessage_id)
WHERE COALESCE(t3.MaxSentDate, t1.SentDate) < CURRENT_DATE - INTERVAL 2 YEAR

Subquery gets last responce date per message.子查询获取每条消息的最后响应日期。

Joining condition joins the most last responce to the message and to all its responces.加入条件将最后一个响应加入到消息及其所有响应中。

Condition checks that this last responce date (for the messages which have no responces - the date of the message itself) it too old for to be removed.条件检查此最后响应日期(对于没有响应的消息 - 消息本身的日期)是否太旧而无法删除。

The query assumes that responce date cannot be earlier than message date.该查询假定响应日期不能早于消息日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM