In my MySQL Database all the entries have a rating(an Integer). Now I want to have a SQL query that delivers 10 elements randomly. And as you probably guess, an element's chance to be taken should be RatingOfThisElement/AllRatings. How can I do that. (I know how to do it in php but I'd like to have a SQL query because of the performance).
Asked
Active
Viewed 4,016 times
3 Answers
0
Google should help you with this: http://akinas.com/pages/en/blog/mysql_random_row/

satnhak
- 9,407
- 5
- 63
- 81
-
That's just randomly. But I want the chance to be RatingOfThisElement/AllRatings, not 1/NumElements like in your example. – user1200226 Apr 04 '12 at 08:46
-
The point is you need to know how to generate randomness. You need to work out how to apply randomness to your situation. – satnhak Apr 04 '12 at 10:06
0
You can use ORDER BY RAND() LIMIT 10 such as here MySQL: Alternatives to ORDER BY RAND() but there are issues with ORDER BY RAND in MySQL if used in certain ways. It can case huge performance issues and you may be better off doing it in PHP - alternatives to ORDER BY RAND

Community
- 1
- 1

liquorvicar
- 6,081
- 1
- 16
- 21
-
That's just randomly,too. But I want the chance to be RatingOfThisElement/AllRatings, not 1/NumElements like in your example. – user1200226 Apr 04 '12 at 08:47
0
here is the solution
SELECT * FROM yourtablename ORDER BY RAND() LIMIT 0,10;

IT ppl
- 2,626
- 1
- 39
- 56
-
If you look at the link I posted that has worse performance than a php query. – satnhak Apr 04 '12 at 08:29
-