0

I'm trying to write a query in Peewee for MySQL and I'd like to do something similar to the solution offered here: Sort by order of values in a select statement "in" clause in mysql

That is, I'd like to select a table using a WHERE clause and an IN operator.

However, rather than have the results ordered based on value(s) found in these tables, I'd like them to arrange them in the same order they're found in the list or operator I provide.

The alternative I'm using now is to simply loop through and accumulate on another list, but this takes much longer (~50-70% more time than just a simple query with an order_by).

Is there a way to do this more elegantly in Peewee?

Denton Zhou
  • 75
  • 1
  • 8

1 Answers1

1

Assuming that information isn't stored in your database, you probably want to just re-order the rows into a new list. This can be done in O(n) for the size of your result-set, so it should not be any slower than just iterating over the rows in the first place.

coleifer
  • 24,887
  • 6
  • 60
  • 75
  • Thanks, @coleifer. That's what I ended up doing as I wasn't sure if this (the FIELD function or a similar alternative) was something Peewee supported. As you mentioned, ordering after querying for results is computationally cheap. The only cost is a couple of extra lines of code, but that's negligible. – Denton Zhou Nov 12 '19 at 00:08