How to tell oracle to sort by a specific sort order passed in from java?

前端 未结 5 625
长情又很酷
长情又很酷 2021-02-04 18:32

Here\'s what I need to be able to do.

I have a List in java which I can convert to comma separate string of IDs like so \"3,4,5,6,1,2\"

I wonder if there\'s way

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-04 19:09

    I don't think it's possible. You can only use ascending or descending order in queries. But what you can do is to use a prepared statement like this select * from t_test t where t.id = ? and run this for each ID in your list and add the result to a result list.

    You could also try to do this with a stored procedure whose parameter would be the list of IDs

    EDIT

    Another idea is to use IN operator on small chunks of your ID-list (maybe 10 in each). As this will return results in unspecified order, you could write a custom comparator or another class that brings this list into your specified order. Then you can concatenate all sub-lists into one result list. For a list with 100 entries and batch size 10 you would only need 10 DB queries + some time for reordering

提交回复
热议问题