在 SQLAlchemy 中选择 Null

发布于 2024-11-02 02:27:39 字数 444 浏览 1 评论 0原文

我想做

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

廻憶裏菂餘溫 2024-11-09 02:27:39

一个更简单的解决方案是使用 sqlalchemy.null():

q1 = session.query(Baz.foo, Quux.bar) \
            .join(Quux.bar)

q2 = session.query(Baz.foo, 
                   sqlalchemy.null().label('null_bar'))

qall = q1.union(q2)
foocol = qall.column_descriptions[0]['expr']
qgrp = qall.group_by([col['name'] for col in qall.column_descriptions])
q = qgrp.having(foocol == 'John Doe')
q.all()

A yet simpler solution is to use sqlalchemy.null():

q1 = session.query(Baz.foo, Quux.bar) \
            .join(Quux.bar)

q2 = session.query(Baz.foo, 
                   sqlalchemy.null().label('null_bar'))

qall = q1.union(q2)
foocol = qall.column_descriptions[0]['expr']
qgrp = qall.group_by([col['name'] for col in qall.column_descriptions])
q = qgrp.having(foocol == 'John Doe')
q.all()
总攻大人 2024-11-09 02:27:39

我设法解决了。解决方案如下所示:

q1 = session.query(Baz.foo, Quux.bar) \
            .join(Quux.bar)

q2 = session.query(Baz.foo, 
                   sqlalchemy.sql.expression.literal_column('NULL as null_bar'))

qall = q1.union(q2)
foocol = qall.column_descriptions[0]['expr']
qgrp = qall.group_by([col['name'] for col in qall.column_descriptions])
q = qgrp.having(foocol == 'John Doe')
q.all()

I managed to work it out. The solution looks like this:

q1 = session.query(Baz.foo, Quux.bar) \
            .join(Quux.bar)

q2 = session.query(Baz.foo, 
                   sqlalchemy.sql.expression.literal_column('NULL as null_bar'))

qall = q1.union(q2)
foocol = qall.column_descriptions[0]['expr']
qgrp = qall.group_by([col['name'] for col in qall.column_descriptions])
q = qgrp.having(foocol == 'John Doe')
q.all()
猫弦 2024-11-09 02:27:39

另一种选择是将 sqlalchemy.text() 与 select 语句一起使用,例如:

import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

mymetadata = sa.MetaData()
Base = declarative_base(metadata=mymetadata)
Session = sessionmaker(bind=sa.engine)

session = Session()

class Person(Base):
    __tablename__ = 'some_table'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(50))


print sa.select([Person.name, sa.text('NULL as null_bar')])

>>> SELECT some_table.name, NULL as null_bar 
FROM some_table

Another option is to use sqlalchemy.text() with a select statement, like:

import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

mymetadata = sa.MetaData()
Base = declarative_base(metadata=mymetadata)
Session = sessionmaker(bind=sa.engine)

session = Session()

class Person(Base):
    __tablename__ = 'some_table'
    id = sa.Column(sa.Integer, primary_key=True)
    name = sa.Column(sa.String(50))


print sa.select([Person.name, sa.text('NULL as null_bar')])

>>> SELECT some_table.name, NULL as null_bar 
FROM some_table
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文