简体   繁体   English

在sqlalchemy,数据库,ORM中使用标签

[英]Use of label in sqlalchemy , database, ORM

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()

So i have this piece of code, and it's working fine but only one thing i would like to add here, and that thing is the use of labels. 所以我有这段代码,并且工作正常,但是我只想在此处添加一件事,那就是标签的使用。 For Exemple how do i use the name label "id1" in my other label? 例如,我如何在其他标签中使用名称标签“ id1”?

I my code to be something like this: 我我的代码是这样的:

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()

Can someone pls explain me how do i use the value of labels inside the query !!! 有人可以解释一下我如何在查询中使用标签的值!!!

You cannot reference the output columns of the parent query in a subquery, though you could reference the parent's source tables: 尽管可以引用父级的源表,但不能在子查询中引用父级查询的输出列:

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

will not work, but 将不起作用,但是

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

does. 做。 You can on the other hand first perform your query for fetching the communications and counts and then add the Persons' organization ids on top. 另一方面,您可以先执行查询以获取通讯和计数,然后在顶部添加人员的组织ID。 Using scalar subqueries the way you've envisioned originally: 使用标量子查询的方式与您最初设想的一样:

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()

With LEFT JOINs: 使用左联接:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM