python - SQLAlchemy: ObjectNotExecuteableError on joins
问题描述
I'm currently struggeling with a statement in SQLAlchemy. Basically the SQL script itself is working and looks like'
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
Now, splitting this statement into SQLAlchemy I've ended up with something like this:
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))
I guess it's because of the distinct. I've also placed it at the end of the full line after outerjoin, but there I get an attribute error, saying that join has no attribute distinct.
Any hints on this for my?
Many thanks in advance, regards, Thomas
解决方案
我自己都不敢相信,但我找到了问题的答案。这或多或少是如何将连接连接在一起的方式。
我上面的查询的解决方案是:
stmt = select([t1.c.col1, t1.c.col2, t2.c.col1, t2.c.col2, t2.c.col3, t3.c.col1]).\
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)))
).distinct()
推荐阅读
- postgresql - 如何在postgresql过程中为多维数组赋值
- c# - C# WPF 如何以编程方式填充我的 DataGrid
- javascript - Axios vs Fetch 如何处理网络错误?
- c# - 有没有办法替换 URL 中已知字符串之前的字符串?
- automation - 如何有条件地使用 waitforxpath?
- ruby-on-rails - 在活动管理员的情况下,操作文本无法将图像存储在 Rich_text_area 中
- c# - DataGrid 行标题和列隐藏
- javascript - 用于 XP 的 Discord.js 排行榜
- php - Laravel 雄辩插入选择
- excel - Excel 恢复默认工作表公式