sql查询,将某个字段用逗号分开,统计里面的字符出现的次数

1、某个博客可能有多个标签,保存的时候,标签时id字符串用“,”拼接起来

 

 

2、现增加一个功能,用来统计每个标签下有多少文章,例如:

 

 3、统计sql

SELECT
	gtag.tagid,
	gtag.numbertag,
	tag.content tagname
FROM
	(
		SELECT
			substring_index(
				substring_index(
					blog.tag_uid,
					',',
					b.help_topic_id + 1
				),
				',' ,- 1
			) AS tagid,
			count(blog.tag_uid) numbertag
		FROM
			test_blog blog
		JOIN mysql.help_topic b ON b.help_topic_id < (
			length(blog.tag_uid) - length(
				REPLACE (blog.tag_uid, ',', '')
			) + 1
		)
		WHERE
			blog.tag_uid != ''
		AND blog.is_deleted = 0
		GROUP BY
			tagid
	) AS gtag
LEFT JOIN blog_tag tag ON tag.uid = gtag.tagid

  

 

posted @ 2022-12-28 11:35  庞某人  阅读(586)  评论(0编辑  收藏  举报