原先有一张表用于存在产品信息
如结构(sql2000):
"产品表"
id   pname
1    AAA
2    BBB
3    CCC
//说明"id"字段是主键(自动编号)
---------------------------------------
原先前台显示是按字段"id"排序显示的。
现在客户的要求是:他想把那个产品放在第一位就第一,他自己去排产品的显示顺序,自然前台也按他排好顺序去显示)。请求大家帮忙,有什么解决方案。(asp.net)

解决方案 »

  1.   

    --先创建一个表 
    create table ai

    id int not null, 
    no varchar(10) not null 

    go --往表中插入数据 
    insert into ai(id, no) values(105, '2') 
    insert into ai(id, no) values(105, '1') 
    insert into ai(id, no) values(103, '1') 
    insert into ai(id, no) values(105, '4')--要求的查询结果如下 
    --即要求no列的数据按'4','1','2'排列 
    id          no        
    ----------- ---------- 
    105         4 
    105         1 
    103         1 
    105         21) 利用函数CHARINDEX
    select * from ai 
    order by charindex(no,'4,1,2')2) 利用函数case并且每组再按照id降序排列
    select * from ai 
    order by case when no='4' then 1 
            when no='1' then 2 
                          when no='2' then 3 
                    end,id desc
    3) 利用UNION 运算符
    select * from ai where no='4' 
    union all select * from ai 
    where no='1' union all 
    select * from ai where no='2'