0
我爲mysql寫了一個查詢,實現了我想要的。它的結構有點像這樣:如何防止sqlalchemy中的嵌套查詢再次選擇表?
select * from table_a where exists(
select * from table_b where table_a.x = table_b.x and exists(
select * from table_c where table_a.y = table_c.y and table_b.z = table_c.z
)
)
我翻譯的查詢SQLAlchemy的,結果的結構是這樣的:
session.query(table_a).filter(
session.query(table_b).filter(table_a.x == table_b.x).filter(
session.query(table_c).filter(table_a.y == table_c.y).filter(table_b.x == table_c.z).exists()
).exists()
)
產生這樣的查詢:
select * from table_a where exists(
select * from table_b where table_a.x = table_b.x and exists(
select * from table_c, table_a where table_a.y = table_c.y and table_b.z = table_c.z
)
)
注在最內層的查詢中重新選擇table_a
- 這打破了預期的功能。
如何停止sqlalchemy再次在嵌套查詢中選擇表格?