在一组结果中获取随机记录

时间:2013-08-08 21:42:05

标签: mysql sql random

我有一个简单的MySQL查询:

 SELECT * ,
 ( MATCH (table.get) AGAINST('playstation ' IN BOOLEAN MODE) ) 
+ ( table.get LIKE '%playstation%') AS _score 
FROM table 
JOIN users on table.id_user = users.id
 WHERE table.expire_datetime > 1375997618
 HAVING _score > 0 
ORDER BY RAND(table.id) ,_score DESC ;

如果我在MySQL中运行此查询,它通常返回多于1条记录,现在我想要LIMIT 1并随机获取其中一条,而不是总是相同的记录。

有可能吗?

3 个答案:

答案 0 :(得分:2)

select * from <my_table> 
ORDER BY RAND()
LIMIT 4

答案 1 :(得分:1)

您将退出播种随机数生成器。我的猜测是它返回遇到的第一个表id,因此数字以相同的顺序生成:

 SELECT * ,
 ( MATCH (table.get) AGAINST('playstation ' IN BOOLEAN MODE) ) 
+ ( table.get LIKE '%playstation%') AS _score 
FROM table 
JOIN users on table.id_user = users.id
 WHERE table.expire_datetime > 1375997618
 HAVING _score > 0 
ORDER BY RAND()
LIMIT 1;

答案 2 :(得分:1)

我理解,_score中的问题? 试试这个:

Select * FROM (
   ***your sql query***
) as t
ORDER BY RAND()
LIMIT 1