SQL Recursive Query

I have a table which looks something like this and sometimes contains 1000s of rows  -

cat_id     cat_parent_id    product_code
1             0                          p1
2             0                          p2
1             0                          p3
3             2                          p4
3             2                          p5
4             3                          p6
4             3                          p7

It represents a hierarchy of categories and the products within each category.

Categories at the root of the tree have a cat_parent_id of 0, categories lower down in the hierarchy have a  cat_parent_id of their container category.

Within each category there are a number of products, and product_code is unique within the table.

I need to be able to count the number of products within a particular subtree,
ie.
      subtree(4)  contains 2 products.
      subtree(2)  contains 5 products, includes products in   categories 2,3 and 4.
      subtree(0)   would contain all products.

Currently I am using a very inefficient query and php to find the desired result. I was wondering if an expert could show me a way to do the same using a recurive SQL query on the table ?

Thanks, Chris Coleman.
Chris ColemanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

aikimarkCommented:
What database are you using?
Ray PaseurCommented:
I think a "recursive query" is not necessarily a good strategy.  If all you have are a few thousand rows, just fetch them all and count the relationships in PHP.  But that said, I'm not quite sure I understand what kind of counts you're looking for.  See the comments for some different ways of looking at the data.  Which kind of structure will give you useful counts?
<?php // demo/temp_chriscoleman.php
/**
 * http://www.experts-exchange.com/questions/28835259/SQL-Recursive-Query.html
 *
 * Some different ways of looking at the relationships
 *
 ****
 *
 * A Tree with branches
 *
 * 0
 * |_1     (p1)
 * |_1     (p3)
 * |_2     (p2)
 *   |_3   (p4)
 *   |_3   (p5)
 *     |_4 (p6)
 *     |_4 (p7)
 *
 ****
 *
 * Minimum paths for all products
 *
 * Chain: 4 -> 3 -> 2 -> 0
 * Count: 2    2    1
 *
 * Chain: 1 -> 0
 * Count: 2
 *
 ****
 *
 * By Product, going backwards up the chain
 *
 * p1: 1 -> 0
 * p2: 2 -> 0
 * p3: 1 -> 0
 * p4: 3 -> 2 -> 0
 * p5: 3 -> 2 -> 0
 * p6: 4 -> 3 -> 2 -> 0
 * p7: 4 -> 3 -> 2 -> 0
 *
 ****
 *
 * By Product set, on identical branches
 *
 * 0 -> 1           (p1, p3)
 * 0 -> 2           (p2)
 * 0 -> 2 -> 3      (p4, p5)
 * 0 -> 2 -> 3 -> 4 (p6, p7)
 *
 */
error_reporting(E_ALL);
echo '<pre>';

// CREATE AN ARRAY OF OBJECTS, LIKE A PDO RESULTS SET
$rows[] = (object)[ 'cat_id' => 1, 'cat_parent_id' => 0, 'product_code' => 'p1' ];
$rows[] = (object)[ 'cat_id' => 2, 'cat_parent_id' => 0, 'product_code' => 'p2' ];
$rows[] = (object)[ 'cat_id' => 1, 'cat_parent_id' => 0, 'product_code' => 'p3' ];
$rows[] = (object)[ 'cat_id' => 3, 'cat_parent_id' => 2, 'product_code' => 'p4' ];
$rows[] = (object)[ 'cat_id' => 3, 'cat_parent_id' => 2, 'product_code' => 'p5' ];
$rows[] = (object)[ 'cat_id' => 4, 'cat_parent_id' => 3, 'product_code' => 'p6' ];
$rows[] = (object)[ 'cat_id' => 4, 'cat_parent_id' => 3, 'product_code' => 'p7' ];

// SHOW THE DATA STRUCTURE
print_r($rows);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dave BaldwinFixer of ProblemsCommented:
I've done this before but I don't know where I put it.  I think your table organization is flawed.  The table organization needs to lead directly to the results you're looking for.  If I can find what I did, I'll post back with more info.
Chris ColemanAuthor Commented:
Ray,

        that makes a lot of sense ..

Actually I was already doing somthing with PHP and multiple DB queries but as you suggested I only need a single query ..

I have done something like this -

$result = $GLOBALS['db']->query("SELECT count(*), CAT.cat_name, INV.cat_id, CAT.cat_parent_id FROM  `" . $dbPrefix . "_inventory` as INV LEFT JOIN `" . $dbPrefix . "_category` as CAT ON (CAT.cat_id = INV.cat_id) AND (INV.available in ('1')) AND (INV.status = 1) group by CAT.cat_id;");  

Which gives me  a table like  you mentioned above.

Needs a bit of tweaking but i needed to respond to the question.

Many Thanks,  Chris.
Chris ColemanAuthor Commented:
After tweaking -

#
## Count visible products by category .
$conditions = " WHERE INV.`product_id` = catIndex.`product_id`";
# Product active.
$conditions .= " AND (INV.status = 1)";
# Hide out of stock ..
$hide_out_of_stock = $GLOBALS['config']->get('config', 'hide_out_of_stock');
$conditions .=  ($hide_out_of_stock === '0')?'':' AND (INV.stock_level > 0)';
# Query.
After tweaking ..

$q = "SELECT COUNT(*) , catIndex.cat_id" .
//
" FROM  `" . $dbPrefix . "_category_index` as catIndex" .
//
" INNER JOIN `" . $dbPrefix . "_inventory` as INV"  .
//
" $conditions " .
//
" group by catIndex.cat_id;";

Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.