【发布时间】:2018-10-10 17:38:04
【问题描述】:
我目前正在为 SQLAlchemy 中的一个语句而苦苦挣扎。基本上 SQL 脚本本身正在运行,看起来像'
SELECT DISTINCT t1.col1, t1.col2, t2.col1, t2.col2, t2.col3, t3.col1
FROM t1
JOIN t2 ON t1.id = t2.id
LEFT JOIN t3 on t1.id = t3.id and t3.col2 = 12345
现在,将此语句拆分为 SQLAlchemy,我最终得到了这样的结果:
stmt = select([t1.c.col1, t1.c.col2, t2.c.col1, t2.c.col2, t2.c.col3, t3.c.col1]).distinct().\
select_from(table(t1)).\
join(table(t2), t1.c.id == t2.c.id).\
outerjoin(table(t3), and_((t1.c.id == t3.c.id), (t3.c.col2 == 12345))
我猜是因为与众不同。我也在outerjoin之后将它放在整行的末尾,但是我得到一个属性错误,说join没有不同的属性。
对我有什么提示吗?
提前非常感谢,问候,托马斯
【问题讨论】:
-
我也省略了 distinct 并试图让它在没有的情况下运行,错误仍然存在,我得到一个 ObjectNotExecutableError。我试图打印它,但
print(str(stmt))返回另一个错误:`'str' object has no attribute '_compiler_dispatch'`
标签: python sql python-3.x select sqlalchemy