mysql 列转行处理

CREATE TABLE `table1` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(255) CHARACTER SET utf8 DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
insert into table1 values(1,'yang,zheng,song');
insert into table1 values(2,'zhao,qian,sun');
insert into table1 values(3,'zhou,wu');

 
SELECT a.id,
substring_index( substring_index( a.name, ',', b.help_topic_id + 1 ), ',',- 1 ) name
FROM table1 a
JOIN mysql.help_topic b ON b.help_topic_id < ( length( a.name ) - length( REPLACE ( a.name, ',', '' ) ) + 1 ) 
ORDER BY a.id


 

猜你喜欢

转载自www.cnblogs.com/linbo3168/p/10176811.html