SQL - order by list order

前端 未结 6 1374
-上瘾入骨i
-上瘾入骨i 2020-12-11 00:38

I have the following query that returns rows based on a comma seperated list

Select * from Table where RecordID in (22,15,105,1,65,32)

I wo

6条回答
  •  清歌不尽
    2020-12-11 01:34

    I'd to the ordering in the client, but if you really want to do it in SQL, do it like this:

    declare @T table (id int identity(1,1), RecordID int)
    
    insert into @T (RecordID)
    values (22), (15), (105), (1), (65), (32)
    
    select * from 
    [table] t 
    inner join @t s on t.id=s.recordid
    where t.id in (22, 15, 105, 1, 65, 32)
    order by s.id
    

    (works in SQL Server 2008)

提交回复
热议问题