Reputation: 906
I want to get 3 random records from my table to 90 times.
Scenario
user_id number_of_bids
12 20
8 40
6 30
what i want is...Get above 3 rows in random order to a specific number In fact it is sum(number_of_bids)
...
And every row should not repeated greater than its number of bids..
I have created a query where I am getting sum of number_of_bids
.Now second query required where these 3 records should be in random order to sum(number_of_bids)
times and every record should not greater repeated greater than its number_of_bids
.
Not sure it can be achieved in one query or not.But you people are experts I am sure you can help me.It will save my execution time.. Thanks..
Upvotes: 2
Views: 128
Reputation: 173602
I would just build an array out of the rows and shuffle it:
$stmt = $db->query('SELECT user_id, number_of_bids FROM table_name', PDO::FETCH_KEY_PAIR);
$results = array(); $startIndex = 0;
foreach ($stmt as $userId => $numberOfBids) {
$results += array_fill($startIndex, $numberOfBids, $userId);
$startIndex += $numberOfBids;
}
shuffle($results);
Then, you can iterate $results
however you'd like.
Upvotes: 1