// Here is our storage array
$ids = array();
// Get available IDs and store it in our array
$q = "SELECT product_id FROM products WHERE product_price > 100 LIMIT 0,100";
$q = mysql_query($q);
while ($r = mysql_fetch_row($q)) {
$ids[] = $r[0];// push new ID
}
// We will store chosen IDs here
$chosen_ids = array();
// Get 30 random rows
$random_n = 30;
while (count($chosen_ids) < $random_n) {// while we want more random data
$index = rand(0, count($ids));// get random index
$chosen_ids[] = $ids[$index];// push random element
unset($ids[$index]);// remove chosen element
}
// We have chosen ids,
// lets grab the data with single execution
// We will make OR clause such as
// WHERE id=3 OR id=8 OR id=9
$or_clause = '';
foreach ($chosen_ids as $id) {
if ($or_clause == '') {
$or_clause .= ' product_id = ' . $id;
} else {
$or_clause .= ' OR product_id = ' . $id;
}
}
// OR clause completed
// Let's read the random rows
$q = " SELECT * FROM products WHERE $or_clause ORDER BY product_id ASC";
$q = mysql_query($q);
while ($r = mysql_fetch_array($q)) {
// here's your random data
print_r($r);
}