How to perform a left join in SQLALchemy?

后端 未结 2 1801
伪装坚强ぢ
伪装坚强ぢ 2020-12-29 22:05

I have a SQL query which perfroms a series of left joins on a few tables:

SELECT

FROM table1 t1
INNER JOIN table2 t2
      ON attr =          


        
相关标签:
2条回答
  • 2020-12-29 22:29

    Here is how to use isouter:

    select_from(db.join(Table1, Table2, isouter=True).join(Table3, isouter=True)) 
    
    0 讨论(0)
  • 2020-12-29 22:54

    The isouter=True flag will produce a LEFT OUTER JOIN which is the same as a LEFT JOIN.

    With your code:

    (sa.select([idc.c.Code])
            .select_from(
                t1.join(t2, and_(t1.c.attr == 1, t2.c.attr2 = 1))
                .join(t3, t3.c.Code == t1.c.Code, isouter=True)))
    

    Declarative example:

    session = scoped_session(sessionmaker())
    session.query(Model).join(AnotherModel, AnotherModel.model_id == Model.id, isouter=True)
    
    0 讨论(0)
提交回复
热议问题