1.表的原始数据如下: SELECT id,warehouse_ids FROM `test` ORDER BY warehouse_ids DESC LIMIT 1 2.根据逗号拆分查询 注意 1.分隔符需要替换成对应的字符,我
SELECT id,warehouse_ids FROM `test` ORDER BY warehouse_ids DESC LIMIT 1
注意
1.分隔符需要替换成对应的字符,我这里的是逗号(,)
2.JOIN的user表是我数据库的一张表,主要的用处是使用他的自增id,自增id必须连续从1开始(1,2,3....)
3.一般会使用Mysql.help_topic表代替user,由于没有这个表的权限使用user代替,
SELECTt.id AS 'id',substring_index( substring_index( t.warehouse_ids, ',', h.id ), ',',- 1 ) AS 'warehouse_id' FROM(SELECT id,warehouse_ids FROM `test` ORDER BY warehouse_ids DESC LIMIT 1) tJOIN user AS h ON h.id-1 < ( char_length( t.warehouse_ids ) - char_length( REPLACE ( t.warehouse_ids, ',', '' ) ) + 1 )
参考了以下sql
SELECT t.id AS 'id', t.NAME AS '姓名', substring_index( substring_index( t.courses, '、', h.help_topic_id + 1 ), '、',- 1 ) AS '课程'FROM t_student t JOIN mysql.help_topic AS h ON h.help_topic_id < ( char_length( t.courses ) - char_length( REPLACE ( t.courses, '、', '' ) ) + 1 );
来源地址:https://blog.csdn.net/weixin_42048982/article/details/130870118
--结束END--
本文标题: MySQL 通过 分隔符 对字段拆分 即一行转多行
本文链接: https://www.lsjlt.com/news/392731.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-29
2024-04-28
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0