0

我有一列是我需要排序的,其中包含字母字符和数字

现在以下值

ABC223 ABC12

现在我只需要通过忽略字符串中的字母字符的数字对其进行排序,

需要记住的是,有时值可以以 GD 开头,有时只有 A 有时 123AB 等

这是可能吗?

4

3 回答 3

2

你可以这样做:

ORDER BY SUBSTRING(YourColumn, 3)
于 2012-10-24T13:10:26.200 回答
1

看看:https ://launchpad.net/mysql-udf-regexp和如何在 MySQL 中进行正则表达式替换?

如果您使用udf,您可以使用正则表达式替换执行 ORDER BY。

于 2012-10-24T13:20:51.960 回答
0

您可以使用uasort对包含该值的数组进行排序。

例如:

$pattern = "/\d+/";
$values = array('ABC123', 'ABC12', 'GD44'); //Just for example

uasort($values, function ($a, $b) use ($pattern)
   {
       $matches = array();
       preg_match($pattern, $a, $matches);
       $a = $matches[0];
       preg_match($pattern, $b, $matches);
       $b = $matches[0];

       return $a - $b;
   });

var_dump($values);

Also you can use udf for manipulating regex using sql query.

于 2012-10-24T13:23:22.597 回答