[Python] Selecting a specific column of a select query

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I defined a Usr sqlalchemy table:

    from sqlalchemy import Column, Integer, String, select
    from sqlalchemy.orm import declarative_base
    Base = declarative_base()
    class Usr(Base): # user table
    __tablename__ = "usr"
    usr_id = Column(Integer, primary_key=True)
    usr_name = Column(String)
    usr_age = Column(Integer)


    Let's say have two queries selecting users on their age or on their name:

    q1 = select(Usr).where(Usr.usr_name.like("%Bob%"))
    q2 = select(Usr).where(Usr.usr_age > 12)


    Now I would like to construct a third query returning users that are returned either by q1 either by q2.

    This obviously doesn't work since q1 and q2 return a 3-tuple:

    q3 = select(Usr).where(Usr.usr_id.in_(q1) | Usr.usr_id.in_(q2))


    Basically I would need something that returns me only a specific column of a select. But I'm stuck here. Any suggestion ?

    Continue reading...

Compartilhe esta Página