0

这是我在这里的第一个问题,所以我希望我做得对。我有以下 SQL 查询:

SELECT *, 
  MATCH(name, descr) AGAINST ('$q') AS score 
FROM songs 
WHERE MATCH (name, descr) AGAINST('$q') 
ORDER BY score DESC

您可能知道,如果描述或名称中存在匹配项,此查询将从songs表中的每一行中选择所有内容。我想要做的是将查询限制为仅搜索最新的 10k 行。我也有一个主键,id.

4

2 回答 2

2

您将需要一个嵌套查询。尝试这个:

SELECT temp.*, 
  MATCH(name, descr) AGAINST ('$q') AS score
FROM (
    SELECT *
    FROM songs 
    ORDER BY id DESC
    LIMIT 10000
    ) temp
WHERE MATCH (name, descr) AGAINST('$q') 
ORDER BY score DESC
于 2013-05-27T15:43:11.490 回答
0

您可以ORDER BY score ASC(而不是DESC)添加LIMIT 10000并最终反转结果的顺序:

SELECT * FROM (
  SELECT *, 
    MATCH(name, descr) AGAINST ('$q') AS score 
  FROM songs 
  WHERE MATCH (name, descr) AGAINST('$q') 
  ORDER BY score ASC
  LIMIT 10000
) ORDER BY score DESC
于 2013-05-27T15:44:35.037 回答