这条sql语句可以这样排序吗 ?

来源:百度知道 编辑:UC知道 时间:2024/06/04 01:43:53
Select * from [guestbook] where guest_id in(5,10,2,3) 怎样令到这条语句按
5
10
2
3
查询的条件来排序呢 ? 这点关系到在网页编程方面的使用,求助高手们;先在此表示感谢了!

Select * from [guestbook] where guest_id in(5,10,2,3) order by
(case when id=5 then 0
case when id=10 then 1
case when id=2 then 2
case when id=3 then 3 end)



Select * from [guestbook] where guest_id =5 union all
Select * from [guestbook] where guest_id=10
Select * from [guestbook] where guest_id=2
Select * from [guestbook] where guest_id=3

--MS SQL用charindex/patindex
Select * from [guestbook] where guest_id in(5,10,2,3) order by patindex('%,'+rtrim(guest_id) +',%',','+@s+',' ) asc

MS SQL 通常是这样用:

declare @s nvarchar(100)
set @s='5,10,2,3'
Select *
from [guestbook]
where ','+@s+',' like '%,'+rtrim(guest_id) +',%'
order by patindex('%,'+rtrim(guest_id) +',%',','+@s+',' ) asc