MySQL Forums
Forum List  »  PHP

Repeated results for rand() LIMIT query.
Posted by: Onkar Shinde
Date: March 18, 2015 02:13PM

Hi,

My query:

$query = "select * from
(
    select * from students
    union all
    select * from students
) as tmp
order by rand() limit 5";

$myquery =   mysqli_query($db_connect, $query);
	while($students = mysqli_fetch_assoc($myquery)){
		$stdid =$students['stdid']; $name = $students['name']; $dept = $students['dept'];
		echo "<br><br>".$stdid."<br>".$name."<br>".$dept;		
	}

There are 20 rows for now. Many will be added later. I get 5 results but sometimes the results are repeated. I mean I get 5 rows, but some rows are repeated. I want the 5 results to be unique. How do I go about it?

Many thanks!

Options: ReplyQuote


Subject
Written By
Posted
Repeated results for rand() LIMIT query.
March 18, 2015 02:13PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.