日期:2014-05-18  浏览次数:20728 次

如何高效的让用户自由调整文章排列顺序,含置顶
文章表Article
其中有一列用于存放排列顺序(假设为NUMBER类型,如需要可用其他类型)
如何利用这一列实现用户对文章的自由排序

比如原文章
A     1
B     2
C     3
我现在的方法是
比如把C文章提前,就必须把C的顺序3改成2,B的顺序改成3
如果是不连续,就要找到前一个最大值(另外当文章有栏目时,我找的最大值还必须是同栏目的,因为顺序仅栏目内有意义)跟它对调
置顶就设成1,取消置顶就改到当前最大值-最后一位
但是这样麻烦,而且也有效率问题,有更好的解决方法吗???



------解决方案--------------------
排序的序号间隔设置为100或更大,这样如果间隔的空间足够的话,其他数据排序根本不需要处理。
------解决方案--------------------
如:

A 100
B 200
C 300


若想把C放在A、B中间,则只更新C的需要为101或100到200之间的数即可。
------解决方案--------------------
排序字段的值不定要是連續的,可以試不連續,只要比最小值小(升序),或者比最大值大(降序)
------解决方案--------------------
所以只要跟新一筆數據而已
------解决方案--------------------
create table Article(col1 varchar(5),col2 int)
insert into Article select 'A ', 1
union all select 'B ', 2
union all select 'C ', 3


select * from Article a
order by case when col1= 'c '
then (select top 1 col2-1 from Article b where b.col2 <a.col2 order by col2 desc)
else col2 end


col1 col2
----- -----------
A 1
C 3
B 2
------解决方案--------------------
一楼有道理~~~偶只为做题
------解决方案--------------------
create table Article(col1 varchar(5),col2 int)
insert into Article select 'A ', 1
union all select 'B ', 2
union all select 'C ', 3
select * from Article
--
create proc dbo.proc_update(@col1 as varchar(5))
as
begin
set nocount on
declare @col1b as varchar(5)
declare @col2a as int
declare @col2b as int
select @col2a=(select col2 from Article where col1=@col1)
select @col2b=(select top 1 col2 from Article where col2 <@col2a order by col2 desc )
select @col1b=(select top 1 col1 from Article where col2 <@col2a order by col2 desc)
-- result
select * from Article where col2 <@col2b
union all
select @col1,@col2b
union all
select @col1b,@col2a
union all
select * from Article where col2> @col2a
end

-- update C lever
dbo.proc_update 'C '

-- drop test
drop proc dbo.proc_update
drop table Article
------解决方案--------------------
不要怕麻烦,相互交换排序序号