提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、MySQL
行转列
1.collect_list (行转列,不去重)
2.collect_set (行转列,去重)
3.explode (Array[] 或 Mapkey:value) 列转行
栗子
select explode(array('A','B','C'))
select explode(map('A',10,'B',20,'C',30))
还是函数使用方便以下内容仅供参考!!!!
二、使用步骤
代码如下(示例):
1.数据库的某一列 su.dept_id
例:123,130 转成
SELECT
substring_index( substring_index( su.dept_id, ',', b.help_topic_id + 1 ), ',',- 1 )
FROM
sys_user su
JOIN mysql.help_topic b ON b.help_topic_id < ((length( su.dept_id ) - length(REPLACE ( su.dept_id, ',', '' )) + 1 ))
WHERE
su.user_id = 32;
总结 : 将数据库的某一列转成多行用集合接受值用于业务。