My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

What do you think of Pagination with an Order By rand() clause?

Lila Fowler's photo
Lila Fowler
·Jan 4, 2019

I'm building an online store as a project, and I think it'll be cool to see new different content with each page refresh i.e. each time a user comes to the website, he'll see different content on the font page even if no new products were added.

I don't know of any ways the code can 'break' or be user 'unfriendly', I haven't checked the possibilities. What do you think?

Here's the query:

SELECT
        SQL_CALC_FOUND_ROWS
        name,
        id
    FROM
        products
    ORDER BY 
        rand() 
    LIMIT
     .........(php calculation to make the pagination work).........

I tested it out and noticed that no duplicates appear across pages (which is the only problem I see with sorting by random), I think that's cause of the super cool pagination-library I'm using. Unless it's just a coincidence....

Nah, it can't be a coincidence across 1,000 rows!

So what do you think?