[mysql] Convert comma-separated field contents into multiple rows and group by

Let’s talk about the requirements first:
The company wants me to export a report through mysql. The content is the number of meetings attended by each person in the company. Now there is a meeting table fusion_meeting. The normal logic is to pass The results can be obtained by directly grouping people by, but our participants are stored in a field by comma separation, which makes it impossible to directly group by.
So we need to convert the comma-separated field contents into multiple rows and then group by

1. Original field format

Insert image description here

2. Convert comma-separated field contents into multiple lines

The sql is given directly below and each step of the sql is explained, which is more helpful for everyone to understand.

The first thing to explain is that mysql.help_topic itself is an information table of mysql, used to store various comments and other help information. help_topic has an id attribute that increases to 1 – help_topic_id, and can be used as a subscript. It has fixed amount of data

explain:

  1. length(a.attendee_uid) - length(REPLACE(a.attendee_uid, ‘,’, ‘’)) + 1
  2. The first step means the length of the field attendee_uid - remove the length of the comma from the field attendee_uid, and then +1 to get how many pieces of data are separated by commas.
  3. For example, if the value obtained in the previous step is 3, then you can determine that this field should be split into 3 lines, help_topic_id<3, which means you can get the subscripts 0,1,2
  4. For example, in this data 'zhangsan, lisi, wangwu', the first substring_index means to separate 'zhangsan, lisi, wangwu' by commas, and then take b.help_topic_id + 1 (help_topic_id is the subscript obtained in step 3). The result is zhangsan
  5. The second substring_index means to take the first one from the right from the result of step 4, because if 'zhangsan,lisi,wangwu' gets the subscript as 2, the result will be 'zhangsan,lisi', so then from the right Taking the first one gives us 'lisi'
SELECT 
	a.id '会议id', 
	a.attendee_uid '原始参会人列表', 
	# 4、比如这条数据'zhangsan,lisi,wangwu'  第一个substring_index的意思就是把'zhangsan,lisi,wangwu'通过逗号分割,
	#    然后取b.help_topic_id + 1(help_topic_id就是第3步得到的下标)结果就是zhangsan
	# 5 第二个substring_index的意思是 再从第4步的结果 从右边取第一个, 因为'zhangsan,lisi,wangwu'如果获取到下标为2的话那得到的就是'zhangsan,lisi'  所以再从右边取第一个就得到了 'lisi'
	substring_index(substring_index(a.attendee_uid, ',', b.help_topic_id + 1), ',', -1) AS '分割后的参会人账号' 
FROM `fusion_meeting` a 
JOIN mysql.help_topic b 
# 1、length(a.attendee_uid) - length(REPLACE(a.attendee_uid, ',', '')) + 1 
# 2、这个的意思是 字段attendee_uid的长度 - 字段attendee_uid去除掉逗号的长度,然后再+1就得到了通过逗号分割后有几条数据
# 3、比如上一步得到是3  那就可以确定这个字段要拆分为3行 help_topic_id<3 也就是可以得到下标 0,1,2
ON b.help_topic_id < length(a.attendee_uid) - length(REPLACE(a.attendee_uid, ',', '')) + 1
WHERE a.hw_conf_id = '969471016';

result:
Insert image description here

3. Group the above results

SELECT uid as '参会人账号',COUNT(*) '参会次数' FROM (
SELECT 
	a.id '会议id', 
	a.attendee_uid '原始参会人列表', 
	substring_index(substring_index(a.attendee_uid, ',', b.help_topic_id + 1), ',', -1) AS uid 
FROM `fusion_meeting` a 
JOIN mysql.help_topic b 
ON b.help_topic_id < length(a.attendee_uid) - length(REPLACE(a.attendee_uid, ',', '')) + 1
WHERE a.hw_conf_id = '969471016'

) c GROUP BY c.uid;

result:
Insert image description here

The article is continuously updated. You can follow the public account below or search "The Last Rosemary" on WeChat to read it immediately to get more complete link information.

Guess you like

Origin blog.csdn.net/qq_38374397/article/details/134337611