在MySQL中订购发票号和字母

时间:2012-09-27 08:48:49

标签: mysql sorting

我在MySQL数据库中对SQL查询结果进行排序时出现问题。我需要一种方法来对与字母和多位数混合的发票号进行排序。

格式为:$ {optional-prefix} $ {number part} $ {optional-postfix},它们都存储在Varchar(32)中。不能更改数字格式,因为值是从多个系统导入的。

我想要排序:(未排序)

  • IoCustTextNoNumber
  • IO-700
  • IO39ABC
  • IO-137-KK
  • IO-037-KK
  • 201-IB
  • 201
  • 38-KK
  • 036
  • 12
  • 11-KE
  • IO-37-KK
  • 00001342
  • IO-36-KK
  • 11-KEK
  • 13
  • 035
  • 37-KK
  • 200
  • IO-701

预期结果:(已排序)

  • 11-KE
  • 11-KEK
  • 12
  • 13
  • 035
  • 036
  • 37-KK
  • 38-KK
  • 200
  • 201
  • 201-IB
  • 00001342
  • IO-36-KK
  • IO-037-KK
  • IO-37-KK
  • IO-137-KK
  • IO-700
  • IO-701
  • IO39ABC
  • IoCustTextNoNumber

任何人都可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:1)

MySQL不会这样做。您可以在PHP之类的东西中构建自定义排序,然后执行for循环并将事物分配给某个位置。或者,您可以选择以lo开头的所有内容,然后更新所有这些内容以将其放入另一列。

在php中,您可以执行以下操作:

foreach($data => row){

   $test = strpos('-', $row); // If this is successful than it has a dash in the string, and it goes towards the front.

  if(!$test) { // If its not a test does it begin with a number.
     if($row[0] >= 0){
         // Do whatever you need
     }
  }

}
相关问题