【发布时间】:2018-07-12 02:33:36
【问题描述】:
我有一个包含 2 列的 postgres 表,用于查询:
start_date_detail: date
flex: jsonb
弹性域示例:
{
"communication": [
{
"remind_on": "mxmw@cxla.nl",
"type": "email",
"remind_date": -150
},
{
"remind_on": "+31612345678",
"type": "sms",
"remind_date": -360
}
]
}
我需要选择所有提醒日期在上周的记录,所以(伪代码):
(now() - 1-week) < (start_date_detail + remind_date) < now()
我如何使用 sqlalchemy 实现这一点?
因为它是查询中的计算值,所以我不知道该怎么做。 在 postgres 中,我想出了这个,并且有效:
SELECT * FROM time_item
WHERE
(start_date_detail + INTERVAL '1 second' * (flex->'communication'->0->>'remind_date')::numeric <= NOW())
OR (start_date_detail + INTERVAL '1 second' * (flex->'communication'->1->>'remind_date')::numeric <= NOW())
如何将其放入 sqlalchemy 中?
还有一点: 在上面的查询中,我将每个通信项添加到 where 子句。我怎样才能使它更灵活?那就是我不需要为每个通信项都添加 where 子句。
【问题讨论】:
标签: python postgresql sqlalchemy