docs(schema): 抖音作品列表把标签展开成 0/1 数值指标
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s

在原有查询上加「每个标签一列,命中 1 / 未命中 0」,可以直接 SUM、算占比、做透视。
保留原来那个逗号拼接的「标签」文本列不动。

列按 `monitor_tag` 词表**动态生成**,以后加标签不用改 SQL —— 实测时库里新加的
「无二无别」已经被自动生成成一列。

实现要点:
* MySQL 5.7 没有 PIVOT,用 GROUP_CONCAT 拼出列定义再 PREPARE;
* 每个指标用 `EXISTS(...)` 子查询 —— 它在 MySQL 里直接返回 1/0,不需要 GROUP BY,
  也就不会把「一行一作品」的查询变成聚合查询;
* 标签名用 `QUOTE()` 转义后拼进比较条件。

验证(生产库 380 行):大字报 96 行、养号 96 行,与标签文本列逐条对得上;
一条作品命中两个标签时两列都为 1(和「多话题命中打多个标签」的语义一致)。
This commit is contained in:
butubb
2026-10-22 13:34:29 +08:00
parent 6c6d4260c5
commit bfa025e4ce
@@ -0,0 +1,78 @@
-- 抖音作品列表 + 标签数值指标
--
-- 在原查询的基础上,把「标签」这个文本列展开成**每个标签一列,命中记 1、没命中记 0**,
-- 这样就能直接 SUM / 算占比 / 做透视:
--
-- 博主昵称 | 标签 | 大字报 | 养号 | 人设 | 广告 | 标题
-- 锋哥苏州同城人脉汇 | 大字报 / 养号 | 1 | 1 | 0 | 0 | ...
--
-- 列是按 `monitor_tag` 词表**动态生成**的 —— 以后加标签不用改这段 SQL。
-- (MySQL 5.7 没有 PIVOT,所以用 GROUP_CONCAT 拼列定义再 PREPARE。)
--
-- 一张作品命中多个标签时,每一列都会是 1 —— 和界面上「多个话题命中就打多个标签」
-- 的语义一致,这些列之间不是互斥的。
--
-- 用法:**整段一次性执行**,不是单条 SELECT。最后那条 EXECUTE 才是结果。
SET SESSION group_concat_max_len = 1000000;
-- 1) 每个标签一列:EXISTS(...) 在 MySQL 里直接返回 1 / 0,不需要 GROUP BY
SET @tag_cols := IFNULL((
SELECT GROUP_CONCAT(
CONCAT(
'EXISTS(SELECT 1 FROM monitor_note_tag nt2',
' JOIN monitor_tag tg2 ON tg2.id = nt2.tag_id',
' WHERE nt2.platform = ''dy'' AND nt2.note_id = n.note_id',
' AND tg2.name = ', QUOTE(tg.name), ') AS `',
REPLACE(tg.name, '`', ''), '`'
)
ORDER BY tg.sort_order, tg.id SEPARATOR ', '
)
FROM monitor_tag tg
), '0 AS `无标签`');
-- 2) 把列定义拼进原查询
SET @sql := CONCAT('
SELECT
COALESCE(st.nickname, n.creator_name, '''') AS 博主昵称,
(SELECT COUNT(*)
FROM monitor_note n2
JOIN monitor_task t5 ON t5.id = n2.task_id
WHERE t5.platform = ''dy''
AND n2.creator_hash <> ''''
AND n2.creator_hash = n.creator_hash) AS 账号作品数,
ca.alias AS 博主备注,
FROM_UNIXTIME(n.published_at / 1000) AS 发布时间,
n.title AS 标题,
n.note_url AS 链接,
m.liked_count AS 点赞,
m.comment_count AS 评论,
m.collected_count AS 收藏,
m.share_count AS 分享,
(SELECT GROUP_CONCAT(t.name ORDER BY t.sort_order SEPARATOR '' / '')
FROM monitor_note_tag nt
JOIN monitor_tag t ON t.id = nt.tag_id
WHERE nt.platform = ''dy'' AND nt.note_id = n.note_id) AS 标签,
', @tag_cols, ',
(SELECT na.alias FROM monitor_note_alias na
WHERE na.platform = ''dy'' AND na.note_id = n.note_id LIMIT 1) AS 作品备注,
FROM_UNIXTIME(n.first_seen_at / 1000) AS 首次采到
FROM monitor_note n
JOIN monitor_task t ON t.id = n.task_id
LEFT JOIN monitor_note_metric m
ON m.id = (SELECT m2.id FROM monitor_note_metric m2
WHERE m2.task_id = n.task_id AND m2.note_id = n.note_id
ORDER BY m2.run_id DESC LIMIT 1)
LEFT JOIN monitor_creator_stat st
ON st.id = (SELECT s.id FROM monitor_creator_stat s
JOIN monitor_task t2 ON t2.id = s.task_id
WHERE t2.platform = ''dy'' AND s.creator_hash = n.creator_hash
ORDER BY s.captured_at DESC, s.id DESC LIMIT 1)
LEFT JOIN monitor_creator_alias ca
ON ca.platform = ''dy'' AND ca.creator_hash = n.creator_hash
WHERE t.platform = ''dy''
ORDER BY n.published_at DESC');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;