关于ORDER BY 指定排列顺序

/*如何让ORDER   BY按指定的顺序排序     
      
  表a里有个列叫Type,是商品类别,就3种情况:S,A,B,如下:   
  id   name   type     
  1     一班   S     
  2     五班   A     
  3     三班   B     
  4     四班   B     
  5     二班   A     
  6     六班   S     
  现在我需要按照‘S’,‘A’,‘B’的顺序排序,如下:   
  1   一班   S     
  6   六班   S     
  5   二班   A     
  2   五班   A     
  3   三班   B     
  4   四班   B     
    
  SELECT   *   
  FROM   tbl_test   
  ORDER   BY   "@#$$%#$%@$@#$@$@#@$这里应该咋写?"   
*/

      
  
select     *     from    a    where    type = ' S '     union     all    
  
select     *     from    a    where    type = ' A '     union     all    
  
select     *     from    a    where    type = ' B '        
  
select    id   ,   name   ,type   
  
from    a   
  
order     by     case    type    when     ' S '     then     1    
  
when     ' A '     then     2     when     ' B '     then     3     else     4     end    (如果对ID排序,则加最后加,id)       
  
select    id   ,   name   ,type   
  
from    ( select     * ,    case    type    when     ' S '     then     1     when     ' A '     then     2     else     3     end     as    seq    from    a)   X   
  
order     by    seq       
  
-- 上诉对ID列没有进行排序,如果在上诉基础上对ID再进行排序。   
   select    id   ,   name   ,type(假设有列id   ,name,   type)   
  
from    ( select     * ,    case    type    when     ' S '     then     1     when     ' A '     then     2     else     3     end     as    seq    from    a)   X   
  
order     by    seq,id     
  
/*1   一班   S     
  6   六班   S     
  2   五班   A     
  5   二班   A     
  3   三班   B     
  4   四班   B     
*/
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值