SQL - retain ordering based on the query params

前端 未结 7 1924
借酒劲吻你
借酒劲吻你 2021-02-10 08:34

I\'m trying to perform a SELECT with an IN clause and I would like to be able to have the results returned in the same order as the elements in my list

7条回答
  •  醉话见心
    2021-02-10 09:06

    You can concatenate your variables and order by instr on it like below. I e cannot vouch for the efficiency of this - but must be Okey.Your front end will obviously have to do a bit more work.But constructing a query like this can be open to sql Injection.

    SELECT * FROM orders WHERE order_no IN ('B123', 'B483', 'B100', 'B932', ...)
    ORDER BY 
      instr ('@B123@'|| '@B483@'||'@B100@'||'@B932@'||... ,'@'|| order_no||'@')
    

提交回复
热议问题