现在我有一张表,想对他的时间字段和季节排序,例如在秋天的时候要把春天和秋天的衣服排在前面,夏天的衣服排在后面,
下面是这张表:
product_ID  product_addtime                  season
1            2010-04-07                       1,3
2            2010-04-06                        2
3            2010-01-31                        1
4            2010-04-20                        3
我想要的结果是:
product_ID  product_addtime                  season
4            2010-04-20                        3
1            2010-04-07                       1,3
3            2010-01-31                        1
2            2010-04-06                        21 代表春,2代表 夏  3代表 秋  

解决方案 »

  1.   

    select * from Table where season in (1,3)union select * from Table where season = 2
      

  2.   

    select *
      from table
     order by 
      IIF(IIF(charindex(season,'1')>0, 
          0, IIF(charindex(season,'3')>0, 1, 2) ASC
      

  3.   


    alter table a add s intupdate a set s = '0' where season = '3' and month(getdate()) in (9,10,11)
    update a set s = '1' where season = '1,3' and month(getdate()) in (9,10,11)
    update a set s = '2' where season = '1' and month(getdate()) in (9,10,11)
    update a set s = '3' where season = '2' and month(getdate()) in (9,10,11)select * from a order by s asc , product_addtime desc
    a是表名,month(getdate()) in (9,10,11) 代表秋天的时候随便写的,LZ参考下
      

  4.   

    select *
      from table
     order by  
      IIF(charindex(season,'1')>0, 0, IIF(charindex(season,'3')>0, 1, 2)),
      product_addtime
      

  5.   

    Select 
    ' 排序字段'=Cast season When '秋天' Then 100 
                When '夏天' Then 1
     
    From a
    Order By 排序字段
      

  6.   


    ;with tbl as (select (Case WHEN season='3' THEN 1 WHEN season='1,3' THEN 2 WHEN season='1' THEN 3 ELSE 4 END) AS lv  from '表'
    )
    SELECT * FROM tbl order by lv ASC