Welcome to Dream.In.Code
Become an Expert!

Join 149,755 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,454 people online right now. Registration is fast and FREE... Join Now!




MySQL: Select random(unseen) row, quickly

 
Reply to this topicStart new topic

MySQL: Select random(unseen) row, quickly

girasquid
17 Feb, 2007 - 11:15 AM
Post #1

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
Hello, all.

I am working on a project that involves storing data in a MySQL database, that users will randomly see. Each time that a user sees the data, we store the id value for the row of data into another table, so that the user won't see that row again.

I know I can select a random row from my first table using this:
CODE

SELECT * FROM data1 ORDER BY RAND() LIMIT 1

And then I could just use the ID value and keep selecting values until I found one that wasn't stored in seen_data. However, this seems...kludgy, and I'd like to know if anyone has a better idea on how I could do this.

Any takers?

Thanks,
Girasquid
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: MySQL: Select Random(unseen) Row, Quickly
17 Feb, 2007 - 12:07 PM
Post #2

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,262



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
I believe you could do this:
SQL
SELECT *
FROM data1 WHERE id
NOT IN (SELECT id FROM seen_data) ORDER BY RAND() LIMIT 1;

User is offlineProfile CardPM
+Quote Post

girasquid
RE: MySQL: Select Random(unseen) Row, Quickly
17 Feb, 2007 - 01:15 PM
Post #3

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
After a quick test, it's working great. Thanks!
User is offlineProfile CardPM
+Quote Post

gregoryH
RE: MySQL: Select Random(unseen) Row, Quickly
22 Feb, 2007 - 12:01 AM
Post #4

D.I.C Regular
Group Icon

Joined: 4 Oct, 2006
Posts: 417


Dream Kudos: 50
My Contributions
QUOTE(skyhawk133 @ 17 Feb, 2007 - 01:07 PM) *

I believe you could do this:
SQL
SELECT *
FROM data1 WHERE id
NOT IN (SELECT id FROM seen_data) ORDER BY RAND() LIMIT 1;


Chris

Love your work... just beautiful, beautiful....
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:57AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month