python - sqlalchemy:从加入的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 查询。。提前谢谢你!!

回答1

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

相似文章

随机推荐

最新文章