【问题标题】:SQL, select entries older than 30 days, filter entries older than 30 days that have entries within the last 30 daysSQL,选择超过 30 天的条目,过滤超过 30 天且在过去 30 天内有条目的条目
【发布时间】:2014-07-11 10:52:09
【问题描述】:

我承认这不是最好的措辞问题。我正在尝试编写一个查询来选择超过 30 天的条目。到目前为止我有这个

SELECT farm_list.customer_id, data_stored.`timestamp`  
FROM farm_db.data_stored data_stored
       INNER JOIN farm_db.farm_list farm_list
          ON (data_stored.farm_code = farm_list.farm_code)
WHERE data_stored.`timestamp` >= CURDATE() - 30
GROUP BY farm_list.customer_id

这可以选择所有超过 30 天的条目并按客户分组。

因此,有些客户在过去 30 天内和超过 30 天内有条目。我想排除那些客户。查询的目的是突出不活跃的客户。每次服务器上有客户活动时,都会更新 data_stored 表。

如果我不清楚,请尽管问,我会尽力澄清。任何帮助将不胜感激。

【问题讨论】:

    标签: mysql sql


    【解决方案1】:

    您的意思是客户的最新时间戳早于 30 天。所以,让我们实现这个:

    SELECT farm_list.customer_id, max(data_stored.`timestamp`) as maxts
    FROM hathor_hb.data_stored data_stored INNER JOIN
         farm_db.farm_list farm_list
         ON (data_stored.farm_code = farm_list.farm_code)
    GROUP BY farm_list.customer_id
    HAVING maxts <= CURDATE() - 30;
    

    【讨论】:

    • 谢谢。不得不稍微调整一下,因为这个查询给了我空的结果。我需要在时间戳上使用 DATE() 函数。
    猜你喜欢
    • 1970-01-01
    • 2020-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2018-08-24
    • 2011-08-24
    相关资源
    最近更新 更多