我有一个select语句,我需要首先按相关性排序,得到前十个相关性,按rand()排序,然后将其限制为1,但我不认为用两个不同的顺序和限制语句可以做到这一点。所以,我的问题是,有没有办法解决这个问题?谢谢。

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

最佳答案

使用子查询:

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;

关于php - 在SQL中具有两个不同的顺序和限制语句,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32619028/

10-09 00:35