0

我从这个站点的一些帖子中得到了这个 mysql 选择,并且 99% 的时间它工作得很好,但有时它不会返回我要求的随机元素的数量,我不知道为什么。这是最近失败的示例:

SELECT DISTINCT movie_title
FROM movies AS r1 JOIN
     (SELECT (RAND() * (SELECT MAX(movie_id) FROM movies)) AS id2
    ) AS r2
WHERE r1.movie_id >= r2.id2 AND movie_title != "New Moon"
ORDER BY r1.movie_id ASC LIMIT 4

答案是这个数组: Array([0] => After Sex,[1] => New Moon) 只有两个元素,而应该是四个。

我在我的电影网站 ( http://www.crosstastemovies.com ) 的测验部分使用它。数据库表有大约 1823 部电影。有人可以对此有所了解吗?

谢谢

4

1 回答 1

1

It is not returning the right number because there are not enough rows. When rand() returns a value very close to 1, the value of id2 is very close to the maximum value. There are not enough rows that satisfy the where clause, so all available are returned.

于 2013-02-11T14:41:55.183 回答