Help with displaying random records with 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?
I'd probably just loop through the result set as usual, but keep a counter of records displayed. Then if the counter is less than 5, reset the result pointer back to the first record, and start cycling through it again until the counter reaches 5. (Of course, check for 0 results first, so you don't get into an infinite loop.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
try something like that
select * from
(SELECT ID, Job, Category
SELECT ID, Job, Category
ORDER BY RAND()
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)