I have a query that select posts randomly created by users with views greater than 1 and not older than 2 weeks which works fine but I noticed that 1 or 2 or more posts have duplicates. I tried to GROUP BY id but still get the same problem. Below is the query;
// the two variables work together with a Jquery load more am using to add 15 more posts on page scroll
$start = 0;
$limit = 15;
$query = "SELECT * FROM posts WHERE views > 1 AND date_created >= SUBDATE(NOW(), INTERVAL 2 WEEK) ORDER BY RAND() LIMIT ".$start.", ".$limit;
$stmt = mysqli_prepare($database, $query);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
// echo posts
}
How do I make the query randomly fetch all posts without duplicating any (which is caused by ORDER BY RAND()
)