mysql根据逗号将一行数据拆分成多行数据

 

转自:https://www.cnblogs.com/David3290/p/11378579.html  

SELECT

  a.id,
  a. NAME,
  substring_index(
    substring_index(
    a.shareholder,',',b.help_topic_id + 1),',' ,- 1) AS shareholder
FROM
  company a
  JOIN mysql.help_topic b ON b.help_topic_id < (length(a.shareholder) - length(REPLACE (a.shareholder, ',', '')) + 1 )  SE

 

 

基本建表语句

CREATE TABLE `company` ( 
  `id` int(20) DEFAULT NULL, 
  `name` varchar(100) DEFAULT NULL, 
  `shareholder` varchar(100) DEFAULT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
INSERT INTO `company` VALUES ('1', '阿里巴巴', '马云'); 
INSERT INTO `company` VALUES ('2', '淘宝', '马云,孙正义');

使用情景:假定a表某一列的数据格式是 逗号分隔 的 a,b,c,想将之变成三行数据

 

 

上一篇:Lodop纯文本英文-等符号自动换行问题


下一篇:oralce 10g 安装后 em提示"数据库状态当前不可用可能是因为数据库的状态为装载或不装载"...