Stranger
Stranger

Reputation: 10611

Select randomly 3 rows equal number of times

I'm creating an advertisement platform for my website, where we need select 3 rows randomly and number of times of banner display also should be balanced.

We can get the random rows like this,

SELECT column FROM table ORDER BY RAND() LIMIT 0,3

and we can balance the number of times by incrementing count field while selecting the row each time and select the rows with less count like this,

SELECT * FROM table ORDER BY display_count LIMIT 0,3.

But it will return the values like 1,2,3,4,5,6 and so. But i need to select rows with minimum count randomly. Any suggestion or idea on this would be great?

Upvotes: 1

Views: 127

Answers (2)

Ricardo Ferreira
Ricardo Ferreira

Reputation: 186

Did you tried

SELECT column FROM table ORDER BY RAND(), display_count LIMIT 0,3

? I think this is what you want.

Upvotes: 0

Omesh
Omesh

Reputation: 29101

is this what you are looking for:

SELECT * 
FROM table 
ORDER BY display_count ASC, RAND() 
LIMIT 0,3;

Upvotes: 1

Related Questions