擅长:python、mysql、java
<p>从<a href="https://stackoverflow.com/users/1286013/francis-p">@Francis P</a>的<a href="https://stackoverflow.com/a/11400460/757449">suggestion</a>中,我想到了这个片段:</p>
<pre><code>q1 = session.\
query(beard.person.label('person'),
beard.beardID.label('beardID'),
beard.beardStyle.label('beardStyle'),
sqlalchemy.sql.null().label('moustachID'),
sqlalchemy.sql.null().label('moustachStyle'),
).\
filter(beard.person == 'bob')
q2 = session.\
query(moustache.person.label('person'),
sqlalchemy.sql.null().label('beardID'),
sqlalchemy.sql.null().label('beardStyle'),
moustache.moustachID,
moustache.moustachStyle,
).\
filter(moustache.person == 'bob')
result = q1.union(q2).all()
</code></pre>
<p>不过,这是可行的,但你不能称之为一个答案,因为它似乎是一个黑客。这是sqlalchemy中应该有<code>RIGHT OUTER JOIN</code>的另一个原因。</p>