Reputation:
I'm trying to select a random entry from my database but only from the latest 100 entries. Any thoughts? Thanks.
Upvotes: 0
Views: 1824
Reputation: 8614
for MySql -
SELECT * FROM
(SELECT * FROM table1 order by created_date desc LIMIT 100) table1_alias
ORDER BY RAND()
LIMIT 1
The inner query here get the top 100 records, you might need to replace created_date with something else.
The outer query is what gives a random record.
For oracle you will need something like this -
select * from
(select * from table1 where rownum < 100 order by created_date desc) table1_alias
where rownum=1 order by dbms_random.value
Upvotes: 1