November 24th, 2012, 04:13 PM
Help with displaying random records in MySQL
I have the following table:
Now, I want to display 5 jobs within the Admin Category, but I want them pulled randomly from the database. ORDER BY RAND won't work because there are only 4 records. I need to display 5 records as it doesn't matter if the same record is displayed more than once. How do I do this?
November 27th, 2012, 01:46 PM
so, this is my test table like yours
I inserted your test data
create table #T
this is a final code for get random 5 records
insert into #T
2, 'Secretary' ,'Admin'
select 3, 'Reception' ,'Admin'
select 4, 'Typist' ,'Admin'
select top 5 t.* from
(select *, FLOOR(RAND() * 1000000) as row from #T
select *, FLOOR(RAND() * 1000000) as row from #T) t
order by row
if you found this post is useful click Give Rep button (bottom side on this reply ) and agree