SQL Server sort by array

velaa5lx  于 2023-05-21  发布在  SQL Server
关注(0)|答案(1)|浏览(146)

My data structure in SQL Server is as follows:

I have a series of IDs that I want to sort my list based on these IDs.

For example:

my_ids = 3,5

My final table will be shown as below:

Is this workable? What is your suggestion for this? Thankful

0ve6wy6x

0ve6wy6x1#

This can be done using the conditional order :

Select *
From mytable
Order by case when id in (3,5) then 1 else 2 end

Demo here

相关问题