简体   繁体   中英

Select random with limit not always working

I got this mysql select from some post here in this site, and 99% of the time it works great, but sometimes it won't return the number of random elements I asked for, and I have no idea why. Here's a recent example that failed:

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

The answer was this array: Array([0] => After Sex,[1] => New Moon) Only two elements when it should have been four.

I'm using this on the quiz section of my movies site ( http://www.crosstastemovies.com ). The database table has around 1823 movies. Can someone please shed some light on this?

Thanks

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM