天天看點

mysql一行資料拆分多行

CREATE DATABASE IF NOT EXISTS `test`
USE `test`;

CREATE TABLE IF NOT EXISTS `t_customers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) DEFAULT NULL,
  `operid` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

DELETE FROM `t_customers`;
INSERT INTO `t_customers` (`id`, `username`, `operid`) VALUES
	(1, '張無忌', '100;200;300');
           
mysql一行資料拆分多行
SELECT
    a.id,
    a.username,
    SUBSTRING_INDEX( SUBSTRING_INDEX( a.operid, ';', b.help_topic_id + 1 ), ';',- 1 ) AS ids 
FROM
    `t_customers` AS a
    JOIN mysql.help_topic AS b ON b.help_topic_id < ( length( a.operid ) - length( REPLACE ( a.operid, ';', '' ) ) + 1 ); 
    
           
mysql一行資料拆分多行
mysql一行資料拆分多行