我有两张桌子:
表pages
:
|id |title |
+---+-----------+
|1 |Foxes |
|2 |Pet dogs |
表keywords
:
|id |page_id |phrase |
+---+--------+----------------------+
|1 |1 |quick brown fox jumps |
|2 |1 |lazy dog |
|3 |2 |caring for a dog |
|4 |2 |pet dog |
我目前正在使用此查询:
SELECT pages.title, MATCH (phrase) AGAINST ('pet dog') as relevance
FROM pages
INNER JOIN keywords
ON pages.id = keywords.page_id
WHERE MATCH (phrase) AGAINST ('pet dog')
ORDER BY relevance DESC;
我得到了这个结果:
|title |relevance |
+------+---------------------+
|Dogs |0.3780859112739563 |
|Foxes |0.015609688125550747 |
|Dogs |0.015609688125550747 |
但我希望每个title
只有一个relevance
,因此每个适用于relevance
的短语的title
应该像这样加在一起:
|title |relevance |
+------+---------------------+
|dogs |0.3936955994 |
|foxes |0.015609688125550747 |
答案 0 :(得分:3)
GROUP BY title
语句,因此相同title
的记录将汇总到一个组中。SUM
函数获取每个组relevance
的总和。因此,此查询可能会帮助您:
SELECT title, SUM(relevance)
FROM
(
SELECT pages.title, MATCH (phrase) AGAINST ('pet dog') as relevance
FROM pages
INNER JOIN keywords
ON pages.id = keywords.page_id
WHERE MATCH (phrase) AGAINST ('pet dog')
ORDER BY relevance DESC
)
GROUP BY title;
答案 1 :(得分:0)
您需要按标题分组并总结相关性分数。以下查询解决了您的问题:
SELECT title, SUM(MATCH (phrase) AGAINST ('pet dog' IN BOOLEAN MODE)) as relevance
FROM pages AS p
INNER JOIN keywords AS k ON p.id = k.page_id
GROUP BY title
HAVING relevance > 0
ORDER BY relevance DESC;
参见 DEMO