小编fab*_*tto的帖子

SQLAlchemy 中带有计数的子查询

鉴于这些 SQLAlchemy 模型定义:

class Store(db.Model):
    __tablename__ = 'store'

    id = Column(Integer, primary_key=True)
    name = Column(String, nullable=False)


class CustomerAccount(db.Model, AccountMixin):
    __tablename__ = 'customer_account'

    id = Column(Integer, primary_key=True)
    plan_id = Column(Integer, ForeignKey('plan.id'), index=True, nullable=False)

    store = relationship('Store', backref='account', uselist=False)
    plan = relationship('Plan', backref='accounts', uselist=False)


class Plan(db.Model):
    __tablename__ = 'plan'

    id = Column(Integer, primary_key=True)
    store_id = Column(Integer, ForeignKey('store.id'), index=True)
    name = Column(String, nullable=False)
    subscription_amount = Column(Numeric, nullable=False)
    num_of_payments = Column(Integer, nullable=False)
    store = relationship('Store', backref='plans')
Run Code Online (Sandbox Code Playgroud)

如何编写查询以按计划获取订阅收入明细?我想获取给定商店的计划列表,以及每个计划的该计划的总收入,通过乘以 Plan.subscription_amount * Plan.num_of_payments * 订阅该计划的客户数量计算

目前我正在尝试使用此查询和子查询:

store …
Run Code Online (Sandbox Code Playgroud)

python sql sqlalchemy

5
推荐指数
1
解决办法
3889
查看次数

标签 统计

python ×1

sql ×1

sqlalchemy ×1