我想将三个表连接在一起,以便找到第三个表的id值。SQL语句正常工作,如下所示:
SELECT table_three.id from table three
JOIN (table_one JOIN table_two ON table_one.id = table_two.table_one_id)
ON table_three.table_two_id = table_two.id
WHERE table_one.id = x AND table_two.external_id = y AND table three.external_id = z
SQL Alchemy代码看起来像这样的东西吗?我看过文档,但是他们的查询和我的不同,我不能很好地理解这个例子,把它应用到我自己的代码中。
证明链接连接语句正是我们一直需要的。
result = connection.execute(
select(Table_three.id)
.join(Table_two, Table_three.Table_two_id == Table_two.id)
.join(Table_one, Table_two.Table_one_id == Table_one.id)
.where(
and_(
Table_three.external_id == z,
Table_two.external_id == y,
Table_on.id = x
)
)
).one_or_none()