List:General Discussion« Previous MessageNext Message »
From:Christian Hammers Date:November 10 2006 7:56am
Subject:Re: ORDER BY RAND() gives me duplicate rows sometimes
View as plain text  

On 2006-11-09 Daevid Vincent wrote:
> I am using this query to pull three random comments from a table:
> 
> "SELECT *, DATE_FORMAT(created_on, '%b %D') as date_format FROM comments
> ORDER BY RAND() LIMIT 3";
> 
> The problem is that sometimes, I get two of the same comment. How can I
> refine this query to give me 3 unique/distinct ones?

Maybe

SELECT DISTINCT 
  rand() as rnd, 
  *, 
  DATE_FORMAT(created_on, '%b %D') as date_format 
FROM 
  comments
ORDER BY 
  rnd
LIMIT 
  3
;

bye,

-christian-
Thread
ORDER BY RAND() gives me duplicate rows sometimesDaevid Vincent10 Nov
  • Re: ORDER BY RAND() gives me duplicate rows sometimesChristian Hammers10 Nov
    • RE: ORDER BY RAND() gives me duplicate rows sometimesJerry Schwartz10 Nov
  • Re: ORDER BY RAND() gives me duplicate rows sometimesuYe10 Nov