这是我的第一个问题,所以我希望我做得对。我有以下SQL查询:
SELECT *,
MATCH(name, descr) AGAINST ('$q') AS score
FROM songs
WHERE MATCH (name, descr) AGAINST('$q')
ORDER BY score DESC
如您所知,此查询会从songs
表中的每一行中选择所有内容,如果说明符或名称中存在匹配项。我想要做的是将查询限制为仅限于例如,搜索最新的10k行。我还有一个主键,id
。
答案 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