【问题标题】:SQLAlchemy - Complex Sub-querying with joinSQLAlchemy - 带有连接的复杂子查询
【发布时间】:2020-03-31 13:17:53
【问题描述】:

我需要能够将其转换为 SQLALchemy,我有点新,但它是必需的。 SQL 查询是这样的:

SELECT name, 
(
    SELECT value from account_settings 
    WHERE name = "max_allowed_records" and accounts.id = account_settings.account_id
) AS max_allowed_records,
(
    SELECT count(*) from employees 
join employeelists on employees.employeelist_id = employeelists.id 
where employeelists.account_id = accounts.id 
group by employeelists.account_id
) AS RECORD_COUNT FROM accounts
having coalesce(max_allowed_records,0) < coalesce(RECORD_COUNT,0);

在 Joined 形式中,我认为它们会产生相同的结果(这样尝试以更好地在 SQLAlchemy Query 中对其进行可视化):

SELECT accounts.name, account_settings.value AS max_allowed_records, count(employees.id) as RECORD_COUNT
FROM accounts
left join account_settings on account_settings.account_id = accounts.id and account_settings.name = "max_allowed_records"
left join employeelists on employeelists.account_id = accounts.id
left join employees on employees.employeelist_id = employeelists.id
group by accounts.id
having coalesce(account_settings.value,0) < coalesce(RECORD_COUNT,0)

所以我在这里想要的是我应该从名称为 max_allowed_records 的帐户的帐户设置中获取值,然后将其与该帐户的员工数量进行比较。不幸的是,帐户和员工之间的唯一链接是员工列表。我想知道你们是否可以指导我在这里需要做什么? SQLAlchemy 加入?还是子查询?

表定义:

class Account(Base):
    __tablename__ = "accounts"

    id = synonym("raw_id")
    name = Column(String(255), nullable=False)
    settings = relationship("AccountSetting")

class AccountSetting(Base):
    __tablename__ = "account_settings"

    id = Column(Integer, primary_key=True)
    account_id = Column(Integer, ForeignKey("accounts.id"))
    name = Column(String(30))
    value = Column(String(1000))

class EmployeeList(Base):
    __tablename__ = "employeelists"

    id = Column(Integer, primary_key=True)
    raw_id = Column("id", Integer, primary_key=True)
    name = Column(String(255))
    account_id = Column(Integer, ForeignKey("accounts.id"))

class Employee(Base):
    __tablename__ = "employees"

    id = synonym("raw_id")
    raw_id = Column("id", Integer, primary_key=True)
    employeelist_id = Column(Integer, ForeignKey("employeelists.id"), nullable=False)

【问题讨论】:

  • 你写过python吗?你得到一个特定的错误吗?此外,如果此查询符合您的要求,您可以使用 odbc 功能简化此开发过程并跳过翻译。
  • 好吧,我现在只有 session.query(accountModel),因为我无法真正开始思考如何开始。比如我如何加入?或者我如何将子查询链接到主查询,就像我在 sql 语句中所做的那样?我得到的通常示例是子查询没有任何基于主查询的过滤器。另外我有条件地将它与其他过滤器一起使用,这就是为什么我不能直接粘贴到 sql 代码中。
  • 您介意将您的表定义放在您的问题上吗?
  • @brddawg 将它们放入其中。希望这些就足够了。我只包括了这个问题所需的列。我希望这不是问题。

标签: python mysql sqlalchemy graphene-python


【解决方案1】:

在整个周末休息之后,我能够将 SQL 查询转换为 SQLAlchemy 查询:

    query = query.outerjoin(max_records_settings,\
            and_(max_records_settings.name == "max_allowed_records",\
                Account.id == max_records_settings.account_id))\
        .outerjoin(EmployeeList, Account.id == EmployeeList.account_id)\
        .add_columns(max_records_settings.value)\
        .join(Employee, EmployeeList.id == Employee.employeelist_id)\
        .group_by(Account.id)\
        .having(coalesce(func.count(Employee.id),0) > (coalesce(max_records_settings.value,0)))\

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2011-10-21
    • 1970-01-01
    相关资源
    最近更新 更多