On Feb 27, 2011, at 8:12 AM, stoneferry wrote:

> Hi I'm wondering if someone could help me with SQLAlchemy and WHERE
> clauses since I'm having trouble putting a query together.
> 
> What I want in my WHERE clause is the below, noting the brackets:
> 
> WHERE
> 
> (game_participation.was_a_sub = %s AND game_participation.was_subbed
>> = %s) OR (game_participation.was_a_sub <= %s AND
> game_participation.was_a_sub >= %s)
> 
> AND game_participation.compid = %s AND game_participation.seasonid =
> %s GROUP BY game_participation.playerid
> 
> The closest I have got to producing this in SQL Alchemy is this:
> 
> statement =
> select([func.count(game_participation.c.idgame_participation),game_participation.c.playerid],
> 
>                                                                 or_(
> 
> (( game_participation.c.was_a_sub == 0) &
> (game_participation.c.was_subbed >= 60 ))
> 
> 
> |
> 
> 
> (( game_participation.c.was_a_sub <= 30) &
> (game_participation.c.was_a_sub >= 1 ))
>                                                                    )
> 
> 
>                                                                 &
> 
> 
> (and_(game_participation.c.compid == compid))
> 
>                                                                 &
> 
> 
> (and_(game_participation.c.seasonid ==
> seasonid)),
> 
> 
> group_by=[game_participation.c.playerid])
> 
> 
> 
> ... This in turn produces this in the WHERE clause which is not
> correct since the four elements in my OR clause area and encased in
> one bracket () only.
> 
> I want ( W & X ) or ( Y & Z ) and i'm getting output of (W and X or Y
> and Z). Full output below:
> 
> WHERE (game_participation.was_a_sub = %s AND
> game_participation.was_subbed >= %s OR game_participation.was_a_sub <=
> %s AND game_participation.was_a_sub >= %s) AND
> game_participation.compid = %s AND game_participation.seasonid = %s
> GROUP BY game_participation.playerid
> 
> Any help would be appreciated.

SQLAlchemy applies parenthesis to the SQL output of an expression based on 
operator precedence rules.      The two phrases are logically equivalent.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to