James Lam
James Lam

Reputation: 1269

How to Find Count After Join in SQLAlchemy

I join multiple tables with:

session.query(R, RR, RRR).join(R).join(RR).all()

I tried:

session.query(func.count(R, RR, RRR)).join(R).join(RR)

However, this does not appear to be the correct approach to determining the count from tables.

I could do

len(session.query(R, RR, RRR).join(R).join(RR).all())

but ideally, I won't have to do my counts in memory.

Upvotes: 0

Views: 91

Answers (1)

van
van

Reputation: 76992

If you are after COUNT(*), below should work:

q = (session
     .query(func.count().label("cnt"))
     .select_from(R)
     .join(RR)
     .join(RRR)
     )
r = q.scalar()

Upvotes: 1

Related Questions