我有一张像这样的桌子:
id ip
-- -----------
1 192.168.2.1
并且需要得到如下结果:
id ip A B C D
-- ----------- --- --- --- ---
1 192.168.2.1 192 168 2 1
可行吗?
我有一张像这样的桌子:
id ip
-- -----------
1 192.168.2.1
并且需要得到如下结果:
id ip A B C D
-- ----------- --- --- --- ---
1 192.168.2.1 192 168 2 1
可行吗?
SELECT `id`, `ip`,
SUBSTRING_INDEX( `ip` , '.', 1 ) AS a,
SUBSTRING_INDEX(SUBSTRING_INDEX( `ip` , '.', 2 ),'.',-1) AS b,
SUBSTRING_INDEX(SUBSTRING_INDEX( `ip` , '.', -2 ),'.',1) AS c,
SUBSTRING_INDEX( `ip` , '.', -1 ) AS d
FROM unicorns
例子
SET @ip = '192.168.1.1';
SELECT @ip,
SUBSTRING_INDEX( @ip , '.', 1 ) AS a,
SUBSTRING_INDEX(SUBSTRING_INDEX( @ip , '.', 2 ),'.',-1) AS b,
SUBSTRING_INDEX(SUBSTRING_INDEX( @ip , '.', -2 ),'.',1) AS c,
SUBSTRING_INDEX( @ip , '.', -1 ) AS d
结果
| @IP | 一个 | 乙| C | D | ---------------------------------- | 192.168.1.1 | 192 | 168 | 1 | 1 |