這工作相當好。然而,在某些情況下,歌曲總數會有舍入誤差(從不超過1)。
/*
* It's more likely to fill the first two arrays from a query, but for
* the example I defined them like
* genreId => value
*/
$genres = array(
1 => 'rock',
2 => 'pop',
3 => 'dance',
4 => 'folk',
27 => 'classical'
);
$pointsPerGenre = array(
1 => 5, //rock, 5 out of 10
2 => 2, //pop, 2 out of 10
3 => 1, //etc...
4 => 10,
27 => 7
);
$totalPoints = array_sum($pointsPerGenre);
$numberOfSongs = 20;
$songsPerPoint = $numberOfSongs/$totalPoints;
$songsPerGenre = array();
foreach(array_keys($genres) as $genreId)
{
$songsPerGenre[$genreId] = round($pointsPerGenre[$genreId] * $songsPerPoint);
}
$queryParts = array();
foreach($songsPerGenre as $genreId => $numberOfSongsPerGenre)
{
$queryParts[] = "(SELECT * FROM TRACKS WHERE genre_id = $genreId ORDER BY RAND() LIMIT $numberOfSongsPerGenre)";
}
$query = implode("\nUNION\n", $queryParts);
這將輸出下面的查詢(在這裏你可以看到舍入誤差,用戶會收到在這種情況下,一個獎金歌曲):
(SELECT * FROM TRACKS WHERE genre_id = 1 ORDER BY RAND() LIMIT 4)
UNION
(SELECT * FROM TRACKS WHERE genre_id = 2 ORDER BY RAND() LIMIT 2)
UNION
(SELECT * FROM TRACKS WHERE genre_id = 3 ORDER BY RAND() LIMIT 1)
UNION
(SELECT * FROM TRACKS WHERE genre_id = 4 ORDER BY RAND() LIMIT 8)
UNION
(SELECT * FROM TRACKS WHERE genre_id = 27 ORDER BY RAND() LIMIT 6)
顯示你的表結構。 –