Bat-Chat
2/6/2016 - 5:32 PM

little random ids with limit in one query

little random ids with limit in one query

"SELECT b.id, b.title
    FROM books AS b
    JOIN
        (SELECT (RAND() *
        (SELECT MAX(id)
        FROM books)) AS rid)
    AS rid2
    JOIN books_genres
        ON books_genres.book_id = b.id
WHERE b.id >= rid2.rid AND b.id <> $bookId
ORDER BY rid2.rid ASC
LIMIT $limit"

SELECT *
FROM random, (
        SELECT id AS sid
        FROM random
        ORDER BY RAND( )
        LIMIT 10
    ) tmp
WHERE random.id = tmp.sid;