Sqlite3 Query

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 ?
 

etienne_marais

Honorary Master
Joined
Mar 16, 2008
Messages
15,093
Decided to use subquery and union across queries. Order by Random() had to be moved to out query.
 

Johnatan56

Honorary Master
Joined
Aug 23, 2013
Messages
30,955
Just curious, why did you have to remove it from the query? You're using MySQLi right? ORDER BY rand()

EDIT: Ah I see SQLite in title, sorry.
 
Top