擅长:python、mysql、java
<p>是的,我们可以。在</p>
<p>请看下面的示例(尤其是<code>correlate</code>方法调用):</p>
<pre><code>from sqlalchemy import select, func, table, Column, Integer
table1 = table('table1', Column('col', Integer))
table2 = table('table2', Column('col', Integer))
subquery = select(
[func.if_(table1.c.col == 1, table2.c.col, None)]
).correlate(table1)
query = (
select([table1.c.col,
subquery.label('subquery')])
.select_from(table1)
)
if __name__ == '__main__':
print(query)
</code></pre>
<p>将导致以下查询</p>
^{pr2}$
<p>如您所见,如果您对<code>select</code>调用<code>correlate</code>,则给定的<code>Table</code>将不会添加到它的<code>FROM</code>-子句中。
即使直接指定<code>select_from</code>,也必须这样做,因为SQLAlchemy很乐意在列中添加它找到的任何表。在</p>