1

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

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 对应于 'photos' 表中的 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 ”。有什么帮助吗?

4

1 回答 1

3

您可以这样做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
于 2013-05-29T11:29:25.950 回答