怎么把查询出的一条数据 的几个字段,放在一个字段上多行显示?:
🚀 思路:
① 首先用 concat 把多个字段拼装在一个字段中
② 然后用 mysql.help_topic 去切割该字段
🚀 过程:
①用 concat 把多个字段拼装在一个字段中(记作为①)
SELECT concat( supervisor_code, ',', construction_code, ',', build_code ) AS VALUE
FROM project_section
WHERE delete_flag=0 and section_code ='GC01-2'
查询结果①:
②用 mysql.help_topic 去切割该字段
SELECT
substring_index( substring_index( ①, ',', b.help_topic_id + 1 ), ',',- 1 ) AS org_no
FROM
①
JOIN mysql.help_topic b
ON b.help_topic_id < ( length( ① ) - length( REPLACE ( ①, ',', '' ))+ 1 )
查询结果②:
🚀 总结:
完整sql:
SELECT
substring_index( substring_index( a.VALUE, ',', b.help_topic_id + 1 ), ',',- 1 ) AS org_no
FROM
(
SELECT concat( supervisor_code, ',', construction_code, ',', build_code ) AS VALUE
FROM project_section
WHERE delete_flag=0 and section_code ='GC01-2'
) AS a
JOIN mysql.help_topic b
ON b.help_topic_id < ( length( a.VALUE ) - length( REPLACE ( a.VALUE, ',', '' ))+ 1 )
版权声明:本文为newuserphb原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。