按自定义规则

时间:2016-07-28 02:58:14

标签: mysql sorting

我知道MySQL支持desc或asc排序数据,但使用默认排序规则不能满足我的要求。

任何人都可以在MySQL中对如下数据进行排序:

| agr_offer_inst                  |
| agr_offer_inst_22_00            |
| agr_offer_inst_22_00_err        |
| agr_offer_inst_22_01            |
| agr_offer_inst_22_01_err        |
| agr_offer_inst_22_02            |
| agr_offer_inst_22_02_err        |
| agr_offer_inst_22_03            |
| agr_offer_inst_22_03_err        |
| agr_offer_inst_22_04            |
| agr_offer_inst_22_04_err        |
| agr_offer_inst_22_05            |
| agr_offer_inst_22_05_err        |
| agr_offer_inst_22_06            |
| agr_offer_inst_22_06_err        |
| agr_offer_inst_22_07            |
| agr_offer_inst_22_07_err        |
| agr_offer_inst_22_08            |
| agr_offer_inst_22_08_err        |
| agr_offer_inst_22_09            |
| agr_offer_inst_22_09_err        |
| agr_offer_inst_err              |
| agr_offer_inst_h                |
| agr_offer_inst_h_22_00          |
| agr_offer_inst_h_22_01          |
| agr_offer_inst_h_22_02          |
| agr_offer_inst_h_22_03          |
| agr_offer_inst_h_22_04          |
| agr_offer_inst_h_22_05          |
| agr_offer_inst_h_22_06          |
| agr_offer_inst_h_22_07          |
| agr_offer_inst_h_22_08          |
| agr_offer_inst_h_22_09          |

以下是我需要的结果:

| agr_offer_inst                  |
| agr_offer_inst_22_00            |
| agr_offer_inst_22_01            |
| agr_offer_inst_22_02            |
| agr_offer_inst_22_03            |
| agr_offer_inst_22_04            |
| agr_offer_inst_22_05            |
| agr_offer_inst_22_06            |
| agr_offer_inst_22_07            |
| agr_offer_inst_22_08            |
| agr_offer_inst_22_09            |
| agr_offer_inst_err              |
| agr_offer_inst_22_00_err        |
| agr_offer_inst_22_01_err        |
| agr_offer_inst_22_02_err        |
| agr_offer_inst_22_03_err        |
| agr_offer_inst_22_04_err        |
| agr_offer_inst_22_05_err        |
| agr_offer_inst_22_06_err        |
| agr_offer_inst_22_07_err        |
| agr_offer_inst_22_08_err        |
| agr_offer_inst_22_09_err        |
| agr_offer_inst_h                |
| agr_offer_inst_h_22_00          |
| agr_offer_inst_h_22_01          |
| agr_offer_inst_h_22_02          |
| agr_offer_inst_h_22_03          |
| agr_offer_inst_h_22_04          |
| agr_offer_inst_h_22_05          |
| agr_offer_inst_h_22_06          |
| agr_offer_inst_h_22_07          |
| agr_offer_inst_h_22_08          |
| agr_offer_inst_h_22_09          |

我已多次尝试,但没有结果。

1 个答案:

答案 0 :(得分:1)

order by可以使用多个键。所以,这是一种方法:

order by (case when col like '%\_h\_%' then 3
               when col like '%\_err' then 2
               else 1
          end),
         col