浅析mysql一行转多行

2019-07-07  本文已影响0人  remote_pluto

一、当前表结构


1.png

二、sql语句

SELECT
 id,
 product_name,
 substring_index( substring_index( product.tags, ',', topic.help_topic_id + 1 ), ',',- 1 ) AS tag
FROM
 tbl_product product
 JOIN mysql.help_topic topic ON topic.help_topic_id < ( length( product.tags ) - length( REPLACE ( product.tags, ',', '' ) ) + 1 );

结果:


2.png

三、解析
1.Substring_index的作用:取得目标字符串左侧第n个分割符左侧的部分,n为负时返回右侧第n个的右部分
2.help_topic 是数据库mysql的一个表,该表提供查询帮助主题给定关键字的详细内容(详细帮助信息)
表字段含义:

ps:这条sql语句其实跟help_topic表没有什么关系,仅仅是借用了help_topic表的help_topic_id的值为(0,1,2,...),因此在本数据库中建立一个仅含ID字段的表进行关联,同样可以实现转多行效果

参考文章:https://blog.csdn.net/m0_37934074/article/details/78784172

上一篇 下一篇

猜你喜欢

热点阅读