繁体   English   中英

sqlalchemy中如何使用join查询多张表

[英]How to query multiple tables using join in sqlalchemy

select count(DISTINCT(a.cust_id)) as count,b.code, b.name from table1 as a inner join table2 as b on a.par_id = b.id where a.data = "present" group by a.par_id order by b.name asc;

如何在 sqlalchemy 中写入以获得预期结果

上面写在 sql 中的查询应该在 sqlalchemy 中是正确的。

感谢您的投入

希望这有效...

session.query(
    func.count(distinct(table1.cust_id)).label('count'),
    table2.code,
    table2.name
).join(
    table2,
    table1.par_id == table2.id
).filter(
    table1.data == "present"
).group_by(
    table1.par_id
).order_by(
    table2.name.asc()
).all()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM