有点像php中的explode('.','1.2.3.4')

解决方案 »

  1.   

    不知道这是不是你要的。
    mysql> select SUBSTRING_INDEX('127.0.0.1','.',1),
        ->  SUBSTRING_INDEX('127.0.0.1','.',2),
        ->  SUBSTRING_INDEX('127.0.0.1','.',-1) \G
    *************************** 1. row ***************************
     SUBSTRING_INDEX('127.0.0.1','.',1): 127
     SUBSTRING_INDEX('127.0.0.1','.',2): 127.0
    SUBSTRING_INDEX('127.0.0.1','.',-1): 1
    1 row in set (0.00 sec)mysql>
      

  2.   

    1、用SQL语句可以,但要建立临时表,适用于符串比较长,内容多;
    2、如果字符串比较短,而且格式固定的话,直接用SUBSTRING_INDEX()取值。
      

  3.   

    我希望的结果是
    ----------------------------
      a  |   b  |   c  |   d
    ----------------------------
     192 |  168 |   0  |   1
    ----------------------------
      

  4.   


    我也尝试过用substring_index。but,在定界符 delim 以及count 出现前,从字符串str返回自字符串。若count为正值,则返回最终定界符(从左边开始)左边的一切内容。若count为负值,则返回定界符(从右边开始)右边的一切内容。如果要用的话,估计要嵌套一个SUBSTRING_INDEX
      

  5.   

    mysql> select
        ->  SUBSTRING_INDEX(@ip,'.',1) as a,
        ->  SUBSTRING_INDEX(SUBSTRING_INDEX(@ip,'.',2),'.',-1) as b,
        ->  SUBSTRING_INDEX(SUBSTRING_INDEX(@ip,'.',3),'.',-1) as c,
        ->  SUBSTRING_INDEX(@ip,'.',-1) as d;
    +------+------+------+------+
    | a    | b    | c    | d    |
    +------+------+------+------+
    | 192  | 168  | 0    | 1    |
    +------+------+------+------+
    1 row in set (0.00 sec)mysql>
      

  6.   

    select substring_index('192.168.0.1','.',-1),
    substring_index(substring_index('192.168.0.1','.',-2),'.',1),
    substring_index(substring_index('192.168.0.1','.',-3),'.',1),
    substring_index(substring_index('192.168.0.1','.',-4),'.',1)
      

  7.   

    select substring_index('192.168.0.1','.',-1) as d, 
    substring_index(substring_index('192.168.0.1','.',-2),'.',1) as c, 
    substring_index(substring_index('192.168.0.1','.',-3),'.',1) as b, 
    substring_index(substring_index('192.168.0.1','.',-4),'.',1) as a
      

  8.   

    mysql> select SUBSTRING_INDEX('1.2.3.4','.',1) as a, substring_index(SUBSTRING_INDEX('1.2.3.4','.',2),'.',-1) as b, substring_index(SUBSTRING_INDEX('1.2.3.4','.',-2),'.',1) as c, substring_index('1.2.3.4','.',-1) as d;
    +---+---+---+---+
    | a | b | c | d |
    +---+---+---+---+
    | 1 | 2 | 3 | 4 |
    +---+---+---+---+
    1 row in set (0.00 sec)
    感觉有点繁琐,有没有简练一点的方法呢?
      

  9.   

    速度上可能会略快一点(猜想),未测试,只是感觉上数学运算比字符运算快。
    mysql> SELECT
        ->  INET_ATON(@ip) div POW(256,3) as a ,
        ->  INET_ATON(@ip) div POW(256,2) % 256 as b ,
        ->  INET_ATON(@ip) div POW(256,1) % 256 as c ,
        ->  INET_ATON(@ip) % 256 as d ;
    +------+------+------+------+
    | a    | b    | c    | d    |
    +------+------+------+------+
    |  192 |  168 |    0 |    1 |
    +------+------+------+------+
    1 row in set (0.00 sec)mysql>
      

  10.   


    就是要找这个函数。。php的ip2long有些算不准唉,没有熟读手册阿