Home > Software engineering >  prevent duplicates when select rows by RAND() and LIMIT
prevent duplicates when select rows by RAND() and LIMIT

Time:09-22

I try to select rows and order them by RAND() and it works fine without any repetitions. But when i limit rows it keeps getting duplicated ones.

Let's say that i have a SHOW MORE button to get more results with AJAX and each click must change the limit (Where should rows start at and how many rows should be fetched). But Here i keep getting duplicated rows.

Here is my line:

SELECT * FROM products 
ORDER BY RAND() LIMIT 20,20

I think that RAND() doesn't care whether LIMIT exists or not, it will always starts fetching rows at anywhere it likes randomly but limit how many rows must be fetched

  • Related