数据库里已有的端口资料类似:serial0/0,serial0/1,serial3/0,serial3/1,ethernet2/0,ethernet2/1到ethernet2/24,如果直接用order by port会出现ethernet排在serial之前,及2/11排在2/2之前类似的问题,我想按照端口编号来排序,结果希望是这样的:0/0,0/1,2/0....2/24,3/0,3/1,谢谢

解决方案 »

  1.   

    用SUBSTRING函数来ORDER BY
    比如:
    ... order by substring(...);
    具体用法看手册:
    SUBSTRING(str,pos), SUBSTRING(str FROM pos), SUBSTRING(str,pos,len), SUBSTRING(str FROM pos FOR len) The forms without a len argument return a substring from string str starting at position pos. The forms with a len argument return a substring len characters long from string str, starting at position pos. The forms that use FROM are standard SQL syntax. It is also possible to use a negative value for pos. In this case, the beginning of the substring is pos characters from the end of the string, rather than the beginning. A negative value may be used for pos in any of the forms of this function. For all forms of SUBSTRING(), the position of the first character in the string from which the substring is to be extracted is reckoned as 1. mysql> SELECT SUBSTRING('Quadratically',5);
            -> 'ratically'
    mysql> SELECT SUBSTRING('foobarbar' FROM 4);
            -> 'barbar'
    mysql> SELECT SUBSTRING('Quadratically',5,6);
            -> 'ratica'        
    mysql> SELECT SUBSTRING('Sakila', -3);
            -> 'ila'        
    mysql> SELECT SUBSTRING('Sakila', -5, 3);
            -> 'aki'
    mysql> SELECT SUBSTRING('Sakila' FROM -4 FOR 2);
            -> 'ki'