如何在 SQLALchemy 中执行左连接?

编程入门 行业动态 更新时间:2024-10-27 17:17:55
本文介绍了如何在 SQLALchemy 中执行左连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 SQL 查询,它在几个表上执行一系列左连接:

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

SELECT <some attributes> FROM table1 t1 INNER JOIN table2 t2 ON attr = 1 AND attr2 = 1 LEFT JOIN table3 t3 ON t1.Code = t2.Code AND t3.Date_ = t1.Date_ LEFT JOIN tabl4 t4 ON t4.Code = t1.code AND t4.Date_ = t1.Date_

到目前为止,我已经:

(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)))

但我不知道如何使加入成为 LEFT JOIN.

but I can't figure out how to make the join a LEFT JOIN.

推荐答案

isouter=True 标志将产生一个 LEFT OUTER JOIN 与 相同>左加入.

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

使用您的代码:

(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)))

声明性示例:

session = scoped_session(sessionmaker()) session.query(Model).join(AnotherModel, AnotherModel.model_id == Model.id, isouter=True)

更多推荐

如何在 SQLALchemy 中执行左连接?

本文发布于:2023-10-16 06:13:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1496725.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   SQLALchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!