Mysql查询 - 仅搜索最新的x行

时间:2013-05-27 15:39:33

标签: mysql

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

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

如您所知,此查询会从songs表中的每一行中选择所有内容,如果说明符或名称中存在匹配项。我想要做的是将查询限制为仅限于例如,搜索最新的10k行。我还有一个主键,id

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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