views:

37

answers:

2

I have a database of filenames that relate many-to-one to a set of subjects. How can I randomly select one filename from the several in the database that relate to a given subject?

I'm working with Zend Framework, so if there is a ZF function that would help please mention it.

+1  A: 
select filename from FileSubjects where subject = "subject" order by rand() limit 1;
Matthew Flaschen
+1  A: 

There's probably a more efficient way, but i've used a mysql query like this before. (Never messed with ZF, so i do it the mysql way...)

SELECT fn.filename
FROM subjects s
INNER JOIN filenames fn ON whatever joins the tables
WHERE the subject id matches
ORDER BY RAND()
LIMIT 1
cHao