Unix Technical Forum

ORDER BY RAND() gives me duplicate rows sometimes

This is a discussion on ORDER BY RAND() gives me duplicate rows sometimes within the MySQL General forum forums, part of the MySQL category; --> I am using this query to pull three random comments from a table: "SELECT *, DATE_FORMAT(created_on, '%b %D') as ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 05:11 AM
Daevid Vincent
 
Posts: n/a
Default ORDER BY RAND() gives me duplicate rows sometimes

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?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 05:11 AM
uYe
 
Posts: n/a
Default Re: ORDER BY RAND() gives me duplicate rows sometimes

Add DISTINCT(primary_key) in your query?

Regards


Willy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:23 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com