Files
MediaCrawler/docs/queries/douyin-latest-video.sql
T
butubb 1b69fa57f2
Deploy VitePress site to Pages / build (push) Canceled after 0s
Deploy VitePress site to Pages / Deploy (push) Canceled after 0s
docs: 抖音每账号最近一条视频的查询
在真库上跑通过(42 个账号)。5.7 没有窗口函数,所以「每个博主最新那条」用相关子
查询挑;账号清单取「作品里出现过的 ∪ 有账号资料的」,否则那些没有作品的号会整行
消失。查询文件里写了这两条的原因,免得下次有人顺手改成 ROW_NUMBER()。
2026-10-11 09:43:56 +08:00

86 lines
4.3 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 抖音所有账号,各自最近一条视频 + 标签 + 详细数据
--
-- 直接在 MySQL 5.7 里跑(生产库 mediacrawler_prod)。已实测:42 个账号,包含
-- **一条作品都没有的**(那些行的作品列是空的)—— 它们出现在列表里是有意的,
-- 和界面上的作品栏一致。
--
-- 为什么写成这个形状:
--
-- * **5.7 没有窗口函数**,ROW_NUMBER() 用不了。所以「每个博主最新那条」是用
-- 相关子查询 `ORDER BY published_at DESC LIMIT 1` 挑的,而不是 rank 之后取 1。
-- * 账号清单取的是 **作品里出现过的 ∪ 有账号资料的**(`UNION` 去重)。只从作品里取
-- 的话,那些「加了目标、也采到了粉丝数、但一条作品都没有」的号会整行消失。
-- * 「最新」按 **published_at**(作者发布时间)排,不是 last_seen_at。降级路径
-- (作品列表被风控挡住)会把所有已知作品都刷一遍,那时 last_seen_at 全都一样,
-- 按它排等于没排。
-- * 指标取**最近一轮**(run_id 最大)的快照。注意抖音作品目前没有 published_at
-- 为空的,所以不存在「没有日期的作品排哪儿」的问题。
--
-- 只想看某一个任务的话,把四处 `t.platform = 'dy'` 换成对应的 task_id,例如:
-- WHERE t1.id = 7
-- (四处指的是:账号清单里两处、最新作品子查询一处、账号快照子查询一处。)
SELECT
acct.creator_hash AS 博主哈希,
ca.alias AS 博主备注,
COALESCE(n.creator_name, st.nickname, '') AS 博主昵称,
st.fans AS 粉丝数,
st.total_favorited AS 总获赞,
st.works_count AS 主页作品数,
n.note_id AS 作品ID,
n.title AS 作品标题,
n.note_url AS 作品链接,
FROM_UNIXTIME(n.published_at / 1000) 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 标签,
(SELECT na.alias FROM monitor_note_alias na
WHERE na.platform = 'dy' AND na.note_id = n.note_id LIMIT 1) AS 作品备注
FROM (
-- 账号清单:作品里出现过的
SELECT n1.creator_hash
FROM monitor_note n1
JOIN monitor_task t1 ON t1.id = n1.task_id
WHERE t1.platform = 'dy'
UNION
-- ∪ 有账号资料的(包含一条作品都没有的博主)
SELECT s1.creator_hash
FROM monitor_creator_stat s1
JOIN monitor_task t2 ON t2.id = s1.task_id
WHERE t2.platform = 'dy'
) acct
-- 这个博主最新的一条作品
LEFT JOIN monitor_note n
ON n.id = (SELECT n2.id
FROM monitor_note n2
JOIN monitor_task t3 ON t3.id = n2.task_id
WHERE t3.platform = 'dy'
AND n2.creator_hash = acct.creator_hash
ORDER BY n2.published_at DESC, n2.id DESC
LIMIT 1)
-- 那条作品最近一轮的指标
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_alias ca
ON ca.platform = 'dy' AND ca.creator_hash = acct.creator_hash
-- 博主最近一轮的账号级指标
LEFT JOIN monitor_creator_stat st
ON st.id = (SELECT s2.id
FROM monitor_creator_stat s2
JOIN monitor_task t4 ON t4.id = s2.task_id
WHERE t4.platform = 'dy'
AND s2.creator_hash = acct.creator_hash
ORDER BY s2.run_id DESC
LIMIT 1)
ORDER BY st.fans DESC, acct.creator_hash