我试图通过不按字母顺序排列的名单来订购商品。完成列表后,我试图按字母顺序继续其余的,而不是我最初选择的那些。
参见示例:
INPUT:
print 'Results:'
select * from Geniuses
order by ('Charles Babbage',
'Albert Einstein',
'Adrien-Marie Legendre',
'Niels Henrik Abel')
然后最后按字母顺序对其余部分进行排序......
输出:
Results:
Charles Babbage ... details
Albert Einstein ...
Adrien-Marie Legendre ...
Niels Henrik Abel ...
Arthur Cayley ...
...
答案 0 :(得分:10)
select * from Geniuses
order by
-- First, order by your set order...
case FullName
when 'Charles Babbage' then 1
when 'Albert Einstein' then 2
when 'Adrien-Marie Legendre' then 3
when 'Niels Henrik Abel' then 4
else 5
end,
-- Then do a secondary sort on FullName for everyone else.
FullName
编辑:
我看到你的评论,每个用户都可以配置它。在这种情况下,您必须有一个FavoriteGeniuses
表,用于跟踪哪个用户更喜欢Geniuses
,然后在该表中指定排序顺序:
select *
from
Geniuses g left join
FavoriteGeniuses fg
ON fg.GeniusID = g.GeniusID
AND fg.UserID = @UserID
order by
-- The higher the number, the first up on the list.
-- This will put the NULLs (unspecified) below the favorites.
fg.SortPriority DESC,
f.FullName
答案 1 :(得分:4)
试试这样:
select * from Geniuses
order by
case when columnName = 'Charles Babbage' then 0
when columnName = 'Albert Einstein' then 1
when columnName = 'Adrien-Marie Legendre' then 2
when columnName = 'Niels Henrik Abel' then 3
else 4
end,
columName