MySQL 查询通过数据透视表返回最常用的标签

标签 mysql tags pivot

我有 3 个表:

threads (thread_id and thread_title)
tags (tag_id and tag_name)
tag_thread (pivot only with thread_id and tag_id)

如何制定一个查询,给出最近 100 个条目中出现次数最多的 5 个标签的名称?

最佳答案

这将为您提供最新 100 个主题的前 5 个标签

select ta.tag_name
from tags ta 
join tag_thread tt on tt.tag_id = ta.tag_id
join 
(
   select * from threads order by thread_id desc limit 100
) th on tt.thread_id = th.thread_id
group by ta.tag_name
order by count(tt.thread_id) desc
limit 5

关于MySQL 查询通过数据透视表返回最常用的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44929459/

相关文章:

javascript - HTML/CSS - 在 img 标签上显示文本

Azure Policy 检查空值

mysql - 转向 SQL Access

Excel 过滤器 - 仅显示过滤器中的相关值

windows-phone-7 - "Refresh"使用 WP7 的 Mvvm-light 工具包进行枢轴控制

mysql - RDS MySQL、Kinesis Firehose 和 Elasticsearch,初始数据加载?

php - 使用 Doctrine DBAL 创建 View 和存储过程

python - Nose 测试运行器找到除标签之外的所有测试

python - 如何为 python 3.5.1 安装 mysql-connector?

php - 使用带有 switch 功能的 foreach