我不确定这个问题是否已在本网站的其他地方得到解答,但我努力用言语解释我的问题。 它在这里: 我想要做的是按照用户选择的标签来订购crawler_results中的文章。因此,文章中出现的标签越多(count_tags越大),它的层次结构就越高。我有4个表格:crawler_results(我存储文章的地方),标签(我在那里存储标签名称),article_tags(我存储tag_id和标签出现的次数)和user_tags(我存储user_id和tag_id )。mysql order by tag occurrence group by
我已经试过:
SELECT cr.id, title, count_tags, t.tag
FROM crawler_results cr
INNER JOIN article_tags at
ON cr.id = at.article_id
INNER JOIN tags t
ON t.id = at.tag_id
INNER JOIN user_tags ut
ON ut.tag_id = at.tag_id
AND user_id = '$user_id'
ORDER BY count_tags DESC
该查询显示由count_tags排序的文章,但不幸的是它与所有的文章包含标签的这样做。例如,如果一篇文章是这样的:“Stackoverflow溢出真棒!”并且用户选择“堆栈”和“溢出”作为标签,“溢出”应该是查询看起来唯一的标签,因为它看起来比“堆栈”更多。
我有一种感觉,它与GROUP BY有关 - 是吗?我只是不知道它是如何工作的。
在此先感谢! 如果您需要更多信息,请让我知道。
编辑: 这里是我的表:
crawler_results:
| id | title | content |
|:-----------|------------:|:------------:|
| 1 | Some title | Some content |
| 2 | Other title | Other content|
标签:
| id | tag |
|:-----------|------------:|
| 1 | Some tag |
| 2 | Other tag |
article_tags:
| id | tag_id | article_id | count_tags |
|:-----------|------------:|:------------:|:------------:|
| 1 | 1 | 1 | 5 |
| 2 | 2 | 2 | 10 |
| 3 | 1 | 2 | 8 |
user_tags:
| id | user_id | tag_id |
|:-----------|------------:|:------------:|
| 1 | 1 | 1 |
| 2 | 1 | 2 |
请参阅REPLACE和LENGTH – Strawberry 2014-10-09 22:42:46
[SQL函数以获取字符串在列中出现次数的次数的可能的重复值](http://stackoverflow.com/questions/3031526/sql-function-to-get-count-字符串出现在列中) – Strawberry 2014-10-09 22:44:15
我有文章中标签的出现次数,现在我只需要通过此次出现来订购它们:) – 2014-10-10 15:40:19