Richard de Koning wrote:

> I created a function where I can loop through a list of words and
> search for these words in a database which looks like:
>
>     for instance in session.query(table).filter(or_(\
>         table.logtext.like(term), table.titlelog.like(term)))\
>         .order_by(desc(table.unixtime)):
>
> I sort the outcome descending on unixtime and later write it to a
> file.
>
> If there are different terms the sorting order isn't working. I can do
> this by storing the outcome in tuples or writing to a temp table, but
> that seems inefficient. I've checked the documentation but cannot find
> anything about it.
>
> Is it possible to merge or append the outcome of different queries
> together?
>   

Is this what you want? The query below ORs all your term clauses
together, so it has the effect of merging the results.

clauses = []
for term in terms:
    clauses.append(table.logtext.like(term))
    clauses.append(table.titlelog.like(term))

q = session.query(table)
if len(clauses) > 0:
    q = q.filter(or_(*clauses))
q = q.order_by(desc(table.unixtime))
# Reduce client memory usage for non-MySQL databases.
q = q.yield_per(1000)
for instance in q:
    ...

-Conor

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

Reply via email to