On Sunday, April 22, 2018 at 11:46:13 AM UTC-4, Carlos Cesar Caballero 
wrote:
>
> Hi guys, I have a list of ids, and I need to obtain the records keeping 
> the list order. In MySQL I can use FIELD() in the "order by" for that. 
> Can I get the ordered records using the DAL?
>

The DAL doesn't have anything built in that will work across database 
backends, but if you are using MySQL, you should be able to do:

db(query).select(orderby='Field(id, %s)' % ', '.join(str(id) for id in 
list_of_ids))

Alternatively, if the list is not too long, you can just do the sorting in 
Python after selection:

db(query).select().sort(lambda r: list_of_ids.index(r.id))

Anthony

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to