If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Selecting a random record from top 100 records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-08-07, 00:49
sqlcc sqlcc is offline
Registered User
 
Join Date: Jan 2007
Posts: 2
Question Selecting a random record from top 100 records

Hope someone can help me with the following issue:

I would like to randomly pick up a row from a table. However the row that is randomly selected should be from the most recently (say, 50 or 100) posted entry in the table.

I was able to randomly pick a record (through rand()) or sort the top entries (through limit). However I am not able to combine both the above requirements.

Thanks in advance for your suggestions!
--
Reply With Quote
  #2 (permalink)  
Old 01-08-07, 02:33
snorp snorp is offline
Registered User
 
Join Date: Apr 2004
Location: Europe->Sweden->Stockholm
Posts: 71
SELECT * FROM (SELECT * FROM tab ORDER BY rank LIMIT 100) x ORDER BY RAND() LIMIT 1

The subselect returns the 100 "top" rows. The outer select returns one of the rows.
Reply With Quote
  #3 (permalink)  
Old 01-08-07, 18:49
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
I'd modify the above, there is no sense selecting all the data in the inner query. just select your primary key.

Code:
select
foo,
bar,
qux
from
(select primarykeycolumn from your table order by rank limit 100) x
order by rand()
limit 1
Reply With Quote
  #4 (permalink)  
Old 01-08-07, 19:58
sqlcc sqlcc is offline
Registered User
 
Join Date: Jan 2007
Posts: 2
Cool Selecting a random record from top 100 records

Thanks snorp & guelphdad for your suggestions.

It is now working beautifully!

--
Reply With Quote
  #5 (permalink)  
Old 01-08-07, 20:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by guelphdad
I'd modify the above, there is no sense selecting all the data in the inner query. just select your primary key.
huh? come again please?

if the subquery selects only the pk, how can you possibly select any other columns in the outer query?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 01-08-07, 23:59
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
ooops, I was in a hurry and was thinking of two different things at once. Yes the above from snorp makes sense. I was thinking of it like this I guess (which doesn't seem any faster of course):

Code:
select
id,
foo,
bar,
qux
from yourtable
where id in (select id from yourtable order by rank limit 100)
order by rand()
limit 1
and of course didn't end up posting that.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On