I have a MySQL table which is called Questions.
Rows in this table have one of their fields called category_name
and I currently have 5 different category names
My question is:
How can I select 2 random rows from each category, and return them as an array?
Obviously I need 10 questions returned back but the way I have it now it might select unequal number of questions between the categories.
Here is my function this far which selects all questions at the moment:
function populateQuestions(){
global $dbc;
$query = "SELECT * FROM Questions";
$result = $dbc->getAll($query);
shuffle($result);
return $result;
}
My category_names
are:
- Meat
- Vegetables
- Fish
- Exotic
- Local
My tables are quite small and the game is not really speed-oriented so I prefer keeping all the questions in one table as they are now.