【发布时间】:2013-01-18 08:16:54
【问题描述】:
我需要生成类似于以下的查询:
(select * from ... where .. and .. order by .. limit ..)
union all
(select * from ... where .. and .. order by .. limit ..)
order by ..
使用 SQLAlchemy,我创建了两个查询对象,如下所示:
q1 = Session.query(..).filter(..).filter(..).order_by(..).limit(..)
q2 = Session.query(..).filter(..).filter(..).order_by(..).limit(..)
q = q1.union_all(q2).order_by(..).all()
但是它不起作用,因为 SQLAlchemy 生成查询:q1 和 q2 不在括号内,它会产生错误。
我怎样才能在括号内为 q1 q2 union 获取这些语句以产生上述查询?
【问题讨论】:
标签: python python-2.7 sqlalchemy