Michael, Once again I appreciate your help on this issue; and once again I cannot seem to find the right recipe to get SQLAlchemy to format the query like I want.
I tried your latest suggestion: >>> users = DBSession.query(Post.id.label('post_id'), >>> Post.user_id.label('user_id')).from_statement(Discussion.__table__.select().with_only_columns(['post_id', >>> >>> 'user_id']).union(DiscussionComment.__table__.select().with_only_columns(['post_id', >>> 'user_id']))) ...but that strips out the first union and skips directly to the last one: >>> print users SELECT post_id, user_id FROM discussions UNION SELECT post_id, user_id FROM discussion_comments I came close by repeating the first select twice: >>> users = DBSession.query(Post.id.label('post_id'), >>> Post.user_id.label('user_id')).from_statement(Post.__table__.select().with_only_columns(['post_id', >>> >>> 'user_id']).union(Discussion.__table__.select().with_only_columns(['post_id', >>> >>> 'user_id']).union(DiscussionComment.__table__.select().with_only_columns(['post_id', >>> 'user_id'])))) Which gives me: >> print users SELECT post_id, user_id FROM posts UNION (SELECT post_id, user_id FROM discussion UNION SELECT post_id, user_id FROM discussion_comments) ...but this is still strange with the added enclosing parenthesis. I'm very sorry to keep belaboring this issue here, but I'm doing a SQL optimization sweep on my code and this query is tripping me up. Thanks, Seth On Sep 8, 6:01 pm, Michael Bayer <mike...@zzzcomputing.com> wrote: > why dont you try: > > query(Post.id, Post.user_id).from_statement(statement1.union > (statement2)) > > ? > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---