python - 在 sqlalchemy、数据库、ORM 中使用标签

标签 python database python-3.x postgresql sqlalchemy

q = session.query(
    label('id1', func.least(Communication.initiator_id, Communication.receiver_id)),
    label('id2', func.greatest(Communication.initiator_id, Communication.receiver_id)),
    label('orga_id1', func.greatest(0, 1)),
    label('orga_id2', func.greatest(0, 1)),
    label('nb', func.count(Communication.id))
).filter(
    or_(
        Communication.initiator_id == people_id,
        Communication.receiver_id == people_id
    )
).order_by("nb").group_by('id1', 'id2').all()

所以我有这段代码,它工作正常,但我只想在这里添加一件事,那就是标签的使用。 例如,我如何在其他标签中使用名称标签“id1”?

我的代码是这样的:

q = session.query(
    label('id1', func.least(Communication.initiator_id, Communication.receiver_id)),
    label('id2', func.greatest(Communication.initiator_id, Communication.receiver_id)),
    label('orga_id1', func.greatest(session.query(People).filter_by(id = "And here somehow i want the value that was created on first label with name 'id1'").first().orga_id, -1)),
    label('orga_id2', func.greatest(session.query(People).filter_by(id = "And here somehow i want the value that was created on second label with name 'id2'").first().orga_id, -1)),
    label('nb', func.count(Communication.id))
).filter(
    or_(
        Communication.initiator_id == people_id,
        Communication.receiver_id == people_id
    )
).order_by("nb").group_by('id1', 'id2').all()

谁能解释一下我如何在查询中使用标签的值!!!

最佳答案

您不能在子查询中引用父查询的输出列,但您可以引用父查询的源表:

select 1 as one, (select one) as two;

不会工作,但是

select 1 as one, (select s.i) as two from generate_series(2, 2) s(i);

确实如此。另一方面,您可以先执行查询以获取通信和计数,然后在顶部添加人员的组织 ID。按照您最初设想的方式使用标量子查询:

id1 = func.least(Communication.initiator_id, Communication.receiver_id)
id2 = func.greatest(Communication.initiator_id, Communication.receiver_id)

sq = session.query(id1.label('id1'),
                   id2.label('id2'),
                   func.count(Communication.id).label('nb')).\
    filter(or_(Communication.initiator_id == people_id,
               Communication.receiver_id == people_id)).\
    order_by('nb').\
    group_by('id1', 'id2').\
    subquery()

orga_id1 = session.query(People.orga_id).filter_by(id=sq.c.id1).as_scalar()
orga_id2 = session.query(People.orga_id).filter_by(id=sq.c.id2).as_scalar()

q = session.query(sq.c.id1,
                  sq.c.id2,
                  func.greatest(orga_id1, -1).label('orga_id1'),
                  func.greatest(orga_id2, -1).label('orga_id2'),
                  sq.c.nb).\
    all()

使用 LEFT JOIN:

from sqlalchemy.orm import aliased

id1 = func.least(Communication.initiator_id, Communication.receiver_id)
id2 = func.greatest(Communication.initiator_id, Communication.receiver_id)

people1 = aliased(People)
people2 = aliased(People)

sq = session.query(id1.label('id1'),
                   id2.label('id2'),
                   func.count(Communication.id).label('nb')).\
    filter(or_(Communication.initiator_id == people_id,
               Communication.receiver_id == people_id)).\
    order_by('nb').\
    group_by('id1', 'id2').\
    subquery()

q = session.query(sq.c.id1,
                  sq.c.id2,
                  func.coalesce(people1.orga_id, -1).label('orga_id1'),
                  func.coalesce(people2.orga_id, -1).label('orga_id2'),
                  sq.c.nb).\
    outerjoin(people1, people1.id == sq.c.id1).\
    outerjoin(people2, people2.id == sq.c.id2).\
    all()

关于python - 在 sqlalchemy、数据库、ORM 中使用标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45461954/

相关文章:

python - 正则表达式的行为不符合预期

python - 将行写入 CSV 文件时为 "ValueError"

database - 如何在 Windows 中使用的 Eclipse 中设置 Prolog Interpreter 路径?

php - 为其他 DBMS 分层 PHP 应用程序

python - 使用 python 类的元胞自动机

python - Django 的 ORM 如何在访问外部对象时设法获取它们

python - AttributeError: 'module'对象没有属性 'spectrogram'

sql - 在 Postgres 中加速慢速 SELECT DISTINCT 查询的解决方案

python - 错误 "TypeError: a bytes-like object is required, not ' int'"

python-3.x - Python : How to turn an IMAGE into a STRING and back?