etienne_marais
Honorary Master
- Joined
- Mar 16, 2008
- Messages
- 15,093
I need to select a specified number of random images/feeds for each category of feeds. At the moment I have it fixed at 4 images/feeds per category but it has to be specifiable per category.
select path,categoryid,description,feedId,feed from feed f inner join category c on f.categoryid=c.id inner join image i on i.feedid=f.id where f.categoryid='1' order by RANDOM() limit 4
select path,categoryid,description,feedId,feed from feed f inner join category c on f.categoryid=c.id inner join image i on i.feedid=f.id where f.categoryid='2' order by RANDOM() limit 4
etc.
I was thinking of using union, but the images must not be grouped per category, the images must scattered across categories. I am using php so I can randomize the result but prefer doing it in sql.
Can anybody help ?
select path,categoryid,description,feedId,feed from feed f inner join category c on f.categoryid=c.id inner join image i on i.feedid=f.id where f.categoryid='1' order by RANDOM() limit 4
select path,categoryid,description,feedId,feed from feed f inner join category c on f.categoryid=c.id inner join image i on i.feedid=f.id where f.categoryid='2' order by RANDOM() limit 4
etc.
I was thinking of using union, but the images must not be grouped per category, the images must scattered across categories. I am using php so I can randomize the result but prefer doing it in sql.
Can anybody help ?