Reputation: 1687
I have a select statement, where I need to first order it by relevance, get the top ten relevance, order it by rand(), and then limit it by 1, but I don't think it's possible to do that with 2 different order, and limit statements. So, my question is, is there a workaround this problem? Thanks.
SELECT id, input, response,
MATCH (
input
)
AGAINST (
'cat frogs trees'
IN NATURAL LANGUAGE
MODE
) AS relevance
FROM allData
HAVING relevance >0
ORDER BY relevance DESC
LIMIT 10
ORDER BY RAND()
LIMIT 1
Upvotes: 2
Views: 66
Reputation: 1270593
Use a subquery:
SELECT t.*
FROM (SELECT id, input, response,
MATCH (input) AGAINST ('cat frogs trees' IN NATURAL LANGUAGE MODE) AS relevance
FROM allData
HAVING relevance > 0
ORDER BY relevance DESC
LIMIT 10
) t
ORDER BY RAND()
LIMIT 1;
Upvotes: 2