在 SQLAlchemy 中选择 Null
我想做
SELECT * FROM
(SELECT foo, bar FROM baz JOIN quux ON baz.id = quux.id
UNION
SELECT foo, NULL AS bar FROM baz)
GROUP BY (foo, bar) HAVING foo = 'John Doe';
与使用 sqlalchemy 0.6 相同的操作,但我似乎无法将 NULL
偷偷放进去。
到目前为止我所拥有的内容大致如下:
q1 = session.query(Baz.foo, Quux.bar).join(Quux)
q2 = session.query(Baz.foo, None)
# ^^^^ This breaks!
I want to do the equivalent of
SELECT * FROM
(SELECT foo, bar FROM baz JOIN quux ON baz.id = quux.id
UNION
SELECT foo, NULL AS bar FROM baz)
GROUP BY (foo, bar) HAVING foo = 'John Doe';
using sqlalchemy 0.6, but I can't seem to sneak that NULL
in there.
This roughly what I have so far:
q1 = session.query(Baz.foo, Quux.bar).join(Quux)
q2 = session.query(Baz.foo, None)
# ^^^^ This breaks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个更简单的解决方案是使用 sqlalchemy.null():
A yet simpler solution is to use sqlalchemy.null():
我设法解决了。解决方案如下所示:
I managed to work it out. The solution looks like this:
另一种选择是将 sqlalchemy.text() 与 select 语句一起使用,例如:
Another option is to use sqlalchemy.text() with a select statement, like: