我的目的如下:
我有一个“StudentID”列表...假设:4、2、3、5、7(例如存储在数组中), 我想创建一个 select 语句,返回列表中指定 studentID 的 StudentID 和 StudentName,与列表的顺序相同。
所以结果应该是:
StudentID StudentName
4 Philip
2 Mary
3 Tima
5 Lara
7 Michel
我怎样才能做到这一点?
最佳答案
我会将 ID 数组放入一个临时表中,然后加入该临时表以进行选择。在临时表中创建标识列将保留所需的顺序。
create table #temp (
SortID int identity,
StudentID int
)
insert into #temp
(StudentID)
select 4 union all
select 2 union all
select 3 union all
select 5 union all
select 7
select s.StudentID, s.StudentName
from StudentTable s
inner join #temp t
on s.StudentID = t.StudentID
order by t.SortID
关于sql - 选择具有特定顺序的特定记录而不使用通用条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4934034/