0

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!

2
Contributors
2
Replies
9
Views
2 Years
Discussion Span
Last Post by JorgeM
0

$myquery = "SELECT * FROM students order by rand(UNIX_TIMESTAMP()) limit 5";

0

Try using the DISTINCT keyword in your query.. for example..

SELECT DISTINCT * FROM ....

Edited by JorgeM

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.