开发者

Select random item where item occurs more than three times in table MySQL

I need to select a random item from a table where a value occurs more th开发者_JS百科an three times in that table.

I've got as far as "SELECT userid FROM breadscores ORDER BY rand() LIMIT 1", but I'm not sure what to put as a where_condition.

I'd also like to ensure that it's weighted evenly regardless of how many more times than three that the entry occurs. Would I have to use some sort of subquery?


SELECT userid FROM breadscores GROUP BY userid HAVING COUNT(*) > 3 ORDER BY RAND() LIMIT 1

Should work.


You'll need to add the limit 1 (I left it off so you can see the select is actually working ;))

SELECT counts.userid 
FROM (SELECT count(*) ucount,userid
  FROM breadscores
  GROUP BY userid) counts
WHERE counts.ucount>3
ORDER BY rand()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜