怎么把查询出的一条数据 的几个字段,放在一个字段上多行显示?:
🚀 思路:
① 首先用 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 )
本文介绍了如何在MySQL中使用CONCAT函数将多个字段合并为一个字段,并通过SUBSTRING_INDEX和LENGTH函数进行切割展示。具体步骤包括首先通过CONCAT将'supervisor_code', 'construction_code', 'build_code'字段合并,然后利用字符串分隔技巧进行逐行显示。

2446

被折叠的 条评论
为什么被折叠?



