zoukankan      html  css  js  c++  java
  • MySQL如何对数据库状态值指定排序

    问题描述:有一个业务表,其状态值有以下几种

    0:"待审批",
    1:"通过",
    2:"不通过",
    3:"驳回",
    4:"委托",

    我的排序规则既不是 order by status desc 也不是 asc 

    而是按照  待审批 > 驳回 > 委托 > 通过 >不通过 的顺序排序

    CREATE TABLE IF NOT EXISTS `test_process` (
          `id` int(11) NOT NULL AUTO_INCREMENT,
          `status` int(11) DEFAULT 0,
    	  `statusDesc` varchar(20) DEFAULT '',
          PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;
    
    insert into test_process (status,statusDesc) value (0,'wait_approval');
    insert into test_process (status,statusDesc) value (1,'pass');
    insert into test_process (status,statusDesc) value (2,'not_pass');
    insert into test_process (status,statusDesc) value (3,'callback');
    insert into test_process (status,statusDesc) value (4,'entrust');
    

    数据如下:

     此时应当使用field函数:

    select * from test_process order by field(status,0,3,4,1) asc;

    输出结果:

    按照后面的值正序排列,无匹配的将会放在最前面(比如2)

     倒序时候,依据值最前面的值的匹配行的放在最后面,无匹配状态值将作为状态值列表的最前面的状态值(2)对应匹配行放在最后面

  • 相关阅读:
    TeX中的引号
    竖式问题
    蛇形填数
    开灯问题
    排列
    分数化小数
    子序列的和
    cookie
    post请求
    get请求
  • 原文地址:https://www.cnblogs.com/xuweiqiang/p/13864978.html
Copyright © 2011-2022 走看看