根据相关性和一个领域进行排序

所以基本上我有两张桌子。 第一张名为“照片”的表格如下所示:

id title
1  Some text here
2  Another text here

而名为'likes'的第二张表看起来像这样

id photos_id user_id
1  1         10
2  1         11
3  2         12

photos_id对应于'照片'表中的ID

我正在使用以下查询来执行全文布尔搜索,然后按相关性进行排序

SELECT *, MATCH (title) AGAINST ('text' in boolean mode)
AS score FROM photos
WHERE MATCH (title) AGAINST ('text' in boolean mode) order by score desc;

根据相关性排序效果很好,但现在我想在相关性相同的情况下按喜欢的总数排序......例如“ 按分数排序,total_likes desc ”。 任何帮助?


你可以为它做Left Join ,并在这里检查SQLFiddle

SELECT
  p.*,
       MATCH (title) AGAINST ('text' IN BOOLEAN MODE) AS score ,
       COUNT(li.id) AS total_likes 
FROM photos p
  LEFT JOIN likes li
    ON p.id = li.photo_id
     WHERE  MATCH (p.title) AGAINST ('text' IN BOOLEAN MODE) 
    GROUP BY 
      li.photo_id

ORDER BY 
      score , total_likes DESC
链接地址: http://www.djcxy.com/p/75289.html

上一篇: sort by relevance and one more field

下一篇: Order by relevance using boolean fulltext search