Ordering by the order of values in a SQL IN() clause

前端 未结 13 795
旧巷少年郎
旧巷少年郎 2020-11-22 04:12

I am wondering if there is away (possibly a better way) to order by the order of the values in an IN() clause.

The problem is that I have 2 queries, one that gets al

13条回答
  •  孤街浪徒
    2020-11-22 04:49

    My first thought was to write a single query, but you said that was not possible because one is run by the user and the other is run in the background. How are you storing the list of ids to pass from the user to the background process? Why not put them in a temporary table with a column to signify the order.

    So how about this:

    1. The user interface bit runs and inserts values into a new table you create. It would insert the id, position and some sort of job number identifier)
    2. The job number is passed to the background process (instead of all the ids)
    3. The background process does a select from the table in step 1 and you join in to get the other information that you require. It uses the job number in the WHERE clause and orders by the position column.
    4. The background process, when finished, deletes from the table based on the job identifier.

提交回复
热议问题