【问题标题】:How to select rows from table using particular difference between two dates?如何使用两个日期之间的特定差异从表中选择行?
【发布时间】:2021-07-09 07:34:37
【问题描述】:

我在 db 中有表“用户”,其中包含以下列:id、url、name、update_at(DateTime)、updated_ext(DateTime)、created_at(DateTime)。

我需要在特定条件下选择所有行:

  1. 如果列 updated_at 中的日期与今天相差超过 1 天(变量名称为 on_start
  2. 如果列 updated_atupdated_ext 之间的差异小于 48 小时(变量的名称是 in_two_days

on_start = (datetime.now() - timedelta(days=1)),

in_two_days = (datetime.now() - timedelta(days=2))

    q = session.query(user).filter(
        user.object_type == object_type,
        or_(
            user.updated_at <= on_start,
            ((user.updated_at - user.updated_external) < in_two_days)
        )
    )

当我尝试运行它时,我得到了

提示:没有运算符匹配给定的名称和参数类型。你 可能需要添加显式类型转换。

因为我试图找出两个日期之间的差异(updated_at 和 updated_ext 以及类似“updated_at_1”的格式:(datetime.datetime(2021, 7, 8, 11, 5, 3, 620971),), ' param_1': datetime.datetime(2021, 7, 7, 11, 5, 3, 620978))。

还有其他比较两个日期的解决方案吗?

非常感谢

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

    有点混乱,但我想我知道你的意思。这应该返回条件为真的索引数组

    df = your_database
    indx_array = where(np.logical_and(user.updated_at <= on_start, (user.updated_at - user.updated_external) < in_two_days))
    

    编辑: 您还可以使用 np.where() 返回条件为真和假的某些值。见https://numpy.org/doc/1.20/reference/generated/numpy.where.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      相关资源
      最近更新 更多