在SQL中有两个不同的顺序和限制语句

时间:2015-09-16 21:26:45

标签: php mysql database mysqli

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

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

1 个答案:

答案 0 :(得分:2)

使用子查询:

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;