Category arranging

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Category arranging

Post by HiddenS3crets »

I have a table that stores categories, but main categories and sub categories.

The table structure has two fields, main_category and category.

- If main_category is blank, then category is the main category.
- If main_category is not empty, then category is a sub-category of main_category

What I want to do is create a query that groups all of the sub-categories for each main category

example:
Main categories: main1, main2
Sub categories: sub1, sub2, sub3

Let's say sub1 is part of main1 while subs 2 & 3 are part of main2

After a query is run I want sub1 to be grouped under main1 and sub2 & 3 to be grouped under main2

Possibly the main categories could be column names and each value could be a sub-category

Code: Select all

main1   |   main2
--------------------
sub1    |   sub2
        |   sub3
Is it possible to structure a query to do this? (or something similar)

Ultimately I want to gather this information into an array with this structure

Code: Select all

array('main1' => array('sub1'), 'main2' => array('sub2', 'sub3'));
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Read the first two threads linked from Useful Posts.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Just so you realise if not immediately obvious to you. Feyd is being nice as the link to Useful Posts is in his signature.
Post Reply