我有一张这样的表,
CREATE TABLE IF NOT EXISTS `cms` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cms` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
INSERT INTO `cms` (`id`, `cms`) VALUES
(1, 'cms:/o:freebsd:freebsd:2.1.7'),
(2, 'cms:/o:netbsd:netbsd:1.0');
我需要将cms列值拆分为四个附加列,因此必须用冒号分隔。
我需要输出类似的东西
需要查询此
答案 0 :(得分:1)
试试这个。它有点脏,但它有效:
select source.*
, left(cms, FirstColon - 1) as CMS2
, mid(cms, FirstColon + 1, SecondColon - FirstColon -1) as Extension
, mid(cms, SecondColon + 1, ThirdColon - SecondColon -1) as Product
, mid(cms, ThirdColon + 1, FourthColon - ThirdColon -1) as version
from (
select a.*
, locate(':', cms) 'FirstColon'
, locate(':', cms, locate(':', cms) + 1) 'SecondColon'
, locate(':', cms, locate(':', cms, locate(':', cms) + 1) + 1) 'ThirdColon'
, locate(':', cms, locate(':', cms, locate(':', cms, locate(':', cms) + 1) + 1) + 1) 'FourthColon'
from cms a
) source
;
但如果您安装了Microsoft Excel,则可以尝试使用Excel Text To Column工具。只需将原始数据导出为Microsoft Excel格式,然后使用该工具。