On Fri, Jan 29, 2016 at 08:51:36AM -0800, Sergei Iakhnin wrote:
> Hi,
> 
> Suppose I have a table Blah(colA int, colB int) and I am using 
> session.query() to query its rows.
> 
> my_results = session.query(Blah.xxx.label("my_result").first() I want to be 
> able to have a python flag such that, depending on its value colA or colB 
> gets selected as the result column. Please help me replace xxx in the query 
> above to achieve this.
> 
> In this toy example I could just copy the query inside an if statement, but 
> the actual query I'm trying to build is large and I don't want to duplicate 
> it.

Hi Sergei,

Perhaps you're looking for something like the following::

    if some_condition:
        query_column = Blah.colA
    else:
        query_column = Blah.colB

    my_result = session.query(query_column.label("my_result")).first()

Cheers,

Michal

> 
> Thanks in advance,
> 
> Sergei.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Attachment: signature.asc
Description: Digital signature

Reply via email to