python - SQLAlchemy 复杂查询

标签 python mysql sql sqlalchemy flask-sqlalchemy

今天我遇到了 sqlalchemy 查询的问题。我已经尝试了一段时间,但我不知道我做错了什么。 我对 sqlalchemy 的整个故事很陌生,我已经开始适应中等简单的查询,但我无法解决以下问题:

units = Unit.query.join(Campaign).join(ServedUnitMetric, and_(ServedUnitMetric.client_id == clientID)).filter(
            (Campaign.active_applications.any(and_(ActiveApplication.application_id == client.application_id, ActiveApplication.enabled == True)))                    &
            (Campaign.targeting_setting.has(TargetingSetting.countries.any(and_(Country.country_code == client.locale.country_code, Country.is_active == True))))     &
            (Campaign.expiration_date > datetime.utcnow())                                                                                                            &
            (Campaign.active_campaign.has(ActiveCampaign.expenses + item.price <= Campaign.budget))                                                                   &
            (Campaign.active_campaign.has(ActiveCampaign.status == CAMPAIGN_STATUS_ACTIVE))                                                                           &
            (Campaign.max_bid >= item.price)                                                                                                                          &
            (Unit.serve_setting.has(and_(ServeSetting.language_code == client.locale.language_code, ServeSetting.valid_from <= datetime.utcnow(), ServeSetting.valid_to >= datetime.utcnow()))) &

        (
            ((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) &  (ServedUnitMetric.unit_id != Unit.id))                                         |
            ((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) &  (ServedUnitMetric.unit_id == Unit.id) & (datetime.utcnow() > (ServedUnitMetric.endDate + ServeSetting.period_hours)))
        )
        )

查询的第一部分正在工作,现在我正在尝试让第二部分工作,但仍然遇到问题。 它总是返回空数据,但我已经准备了测试,并且我确信它应该返回数据。

我收到错误,但我仍然不知道如何解决。问题就出在这里

ServedUnitMetric.endDate + ServeSetting.period_hours

看起来 SQLAlchemy 没有正确转换 DateTime + Interval。看起来这句话被翻译成 double 值。现在的重点是了解如何将 DateTime 添加到 Interval 中,并且应该完成该技巧。 我正在尝试使用 func.adddate 但它会溢出,我想是由于间隔是 1970-1-1 + 您输入的间隔。

感谢https://stackoverflow.com/a/17236032/956541,我终于找到了一个似乎有效的解决方案。

使该行变为:

((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) & (ServedUnitMetric.unit_id == Unit.id) & (func.abs(func.unix_timestamp(datetime.utcnow()) - func.unix_timestamp(ServedUnitMetric.endDate)) >= (ServeSetting.period_hours * 60 * 60)))

感谢您的帮助,

恩里科

最佳答案

解决方案是 func.timestamp(date)

((Unit.serve_setting.has(ServeSetting.serve_once_per_period == True)) & (ServedUnitMetric.unit_id == Unit.id) & (func.abs(func.unix_timestamp(datetime.utcnow()) - func.unix_timestamp(ServedUnitMetric.endDate)) >= (ServeSetting.period_hours * 60 * 60)))

感谢https://stackoverflow.com/a/17236032/956541

关于python - SQLAlchemy 复杂查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24558252/

相关文章:

python - 无法创建 virtualenv——错误的 python 路径?

mysql - 自更新sql查询的问题

java - 在与 MySQL 的 JDBC 连接中,环境变量 OPENSHIFT_MYSQL_DB_HOST 不起作用

python - 属性错误: module 'tensorflow.python.framework.tensor_shape' has no attribute 'scalar'

javascript - 表单数据未发送到服务器

使用 MySQL Workbench 添加外键时出现 MySQL 错误 1064

sql - 如何使用多个 INNER JOIN 加速查询

mysql - 在一个查询中选择记录并更新

mysql - MATCH AGAINST 不适用于两个 LEFT JOIN : #1210 - Incorrect arguments to MATCH

python - 如何对列表中的两个最大元素求和?