我有一个网站,可以从数据库中获取随机电影,然后将其显示给您。我想选择它而不重复(在没有显示其他电影时几次显示一部电影)并以最快的方式完成。
这就是我所拥有的:
$db = new mysqli($DBhost, $DBuser, $DBpassword, $DBdatabase);
$offset_result = " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM database";
$result = $db->query($offset_result);
$offset_row = $result->fetch_object();
$offset = $offset_row->offset;
$db = new mysqli($DBhost, $DBuser, $DBpassword, $DBdatabase);
$query = "SELECT id, title, year, front_image, category, watched, description, imdb_code, bg_image, scene_warning FROM database LIMIT $offset,1";
$stmt = $db->query($query);
list($id, $mtitle, $myear, $front_image, $category, $watched, $description, $imdb_code, $bg_image, $warning)=$stmt->fetch_row();
这是选择但重复...不重复,我有这个,但它很慢。
$db = new mysqli($DBhost, $DBuser, $DBpassword, $DBdatabase);
$query = "SELECT id, title, year, front_image, category, watched, description, imdb_code, bg_image, scene_warning FROM a358_filmovi WHERE id NOT IN (".$movies_array.") ORDER BY rand() LIMIT 1";
$stmt = $db->query($query);
list($id, $mtitle, $myear, $front_image, $category, $watched, $description, $imdb_code, $bg_image, $warning)=$stmt->fetch_row();
$ movies_array是包含电影ID的内爆会话数组。
所以,问题是,如何使查询与第一个(最快的一个)相同,但是使用包含ID的数组,以便电影不会重复?
答案 0 :(得分:4)
为什么不首先将所有id拉入会话数组,选择随机索引,查找,然后从会话数组中删除该索引?