I dont even know if this is possable but here goes:
I have two tables for a project im working on...
Category
SubCategory
SubCategory is 'joined' to Category by the primary key of Category (Category_ID).
Here is what i want to do, I want to select ALL Categories.. AND ALL SubCategories, but grouped first by Category, and then by SubCategory.
Heres the catch... not all Categories have a SubCategory.
Here is the eventual output I want to generate:
Category 1
Category 2
Category 3
-- Subcategory 2
-- SubCategory 4
Category 4
-- SubCategory 1
Category 5
-- SubCategory 6
-- SubCategory 3
-- SubCategory 5
Here is how i would do it in PHP:
$rCategory = mysql_query("SELECT * FROM Category ORDER BY Category_Name");
while ($aCategory = mysql_fetch_array($rCategory)) {
print $aCategory['Category_Name'],"<br />\n";
$rSubCategory = mysql_query("SELECT * FROM SubCategory WHERE Category_ID='".$aCategory['Category_ID']."' ORDER BY SubCategory_Name");
while ($aSubCategory = mysql_fetch_array($rSubCategory)) {
print "-- ".$aSubCategory['SubCategoryName']."<br />\n";
}
}
The goal is to optimize this into ONE mysql_query and work out the same result in PHP....
Any chance this is possable?
:Figment