繁体   English   中英

sqlalchemy:从连接表中选择

[英]sqlalchemy: select from joined table

我如何在 sqlalchemy 中编写这个 sql 查询?

SELECT
    n.name
FROM
    project_nomenclatures pn
    JOIN nomenclatures_sections ns on pn.nomenclature_id = ns.nomenclature_parent_id
    JOIN nomenclatures n on ns.nomenclature_id = n.id
WHERE
    pn.id = 2018

我试过这个:

        sql = (
            select(Nomenclature)
            .join(NomenclatureSection, and_(
                NomenclatureSection.nomenclature_id == Nomenclature.id,
                NomenclatureSection.nomenclature_parent_id == ProjectNomenclature.nomenclature_id
            ))
            .join(ProjectNomenclature, ProjectNomenclature.id == 2018)
        )

然后我尝试了这个:

        sql = (
            select(ProjectNomenclature, Nomenclature)
            .join(NomenclatureSection, NomenclatureSection.nomenclature_parent_id == ProjectNomenclature.nomenclature_id)
            .join(Nomenclature, NomenclatureSection.nomenclature_id == Nomenclature.id)
            .where(ProjectNomenclature.id == 2018)
        )

我什至试过这个

        sql = (
            select(Nomenclature)
            .join(NomenclatureSection, NomenclatureSection.nomenclature_parent_id == ProjectNomenclature.nomenclature_id)
            .join(ProjectNomenclature, NomenclatureSection.id == ProjectNomenclature.nomenclature_id)
            .where(ProjectNomenclature.id == 2018)
        )

但没有任何效果:((我不知道如何在 sqlalchemy 中编写这个 sql 查询.. 提前谢谢你!!

碰巧,我不得不使用 select_from() 函数

暂无
暂无

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

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