473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

basic recursive query (but not for me obviously)

I need to find toplevel image categories and a) number of images directly in
them and b) number of subcategories directly in those topcategories. In
toplevel image categories "icat_parent_id IS NULL". Below is also current
erraneous draft but it has ambigous filed name problem.

CREATE TABLE icategories (
icat_id int(11) NOT NULL auto_increment,
icat_parent_id int(11) default NULL,
icat_name char(100) default NULL,
PRIMARY KEY (icat_id),
KEY icat_parent_id (icat_parent_id),
KEY icat_name (icat_name)
) TYPE=MyISAM;
CREATE TABLE images (
img_id int(11) NOT NULL auto_increment,
img_icat_id int(11) default NULL,
img_data mediumblob,
PRIMARY KEY (img_id),
KEY img_icat_id (img_icat_id)
) TYPE=MyISAM;
SELECT
icat_id AS parent_id,
icat_name AS parent_name,
count( img_id ) AS img_count,
IF (count( children.icat_id ) >0, count( children.icat_id ) , 0) AS
cat_count
FROM icategories
LEFT OUTER JOIN images ON img_icat_id = parent_id
LEFT OUTER JOIN icategories AS children ON icat_parent_id = parent_id
WHERE icategories.icat_parent_id IS NULL
GROUP BY parent_id
ORDER BY parent_name ASC

--
Perttu Pulkkinen
www.risteys.net
www.onnenliinat.com
www.palvelukaupunki.fi
Jul 20 '05 #1
2 6999
Perttu Pulkkinen wrote:
I need to find toplevel image categories and a) number of images directly in
them and b) number of subcategories directly in those topcategories.


I liked Joe Celko's book "SQL for Smarties", in which he talks about
representing recursive relationships in SQL.

The method he describes is to create another table to record paths in
the "tree":

CREATE TABLE pathenum (
icat_ancestor_id int(11) NOT NULL,
icat_descendant_id int(11) NOT NULL,
pathlength int(11) NOT NULL,
KEY icat_ancestor_id (icat_ancestor_id),
KEY icat_descendant_id (icat_descendant_id),
);

This is instead of recording parent_id in your icategories table.

Populate all paths in the tree, for instance if A -> B -> C, then
you record (A,B,1), (B,C,1), and (A,C,2).

Then you can find toplevel category ids with:

SELECT DISTINCT P.icat_ancestor_id
FROM pathenum P LEFT OUTER JOIN pathenum P2
ON (P.icat_ancestor_id = P2.icat_descendant_id)
WHERE P2.icat_descendant_id IS NULL

You can find all the children of a given toplevel category with:

SELECT P.icat_descendant_id
FROM pathenum P
WHERE P.icat_ancestor_id = ?

Regards,
Bill K.
Jul 20 '05 #2
I've handled a number of recursive/tree situations with a LAMP-based
project (http://libdata.sourceforge.net/). This project builds
data-driven web pages with parent-child relationships. I do it this
way. Here's a subset of the fields I use to draw the pages:

* element_id
* page_id
* parent_id (references another element_id)
* generation (denormalized, represents how deep the recursion would go
if conducted)
* element_order (unique integer 1-N for each element on a given page)
* other fields to store the node data...

Distincts and complex joins are your enemy with large recordsets.
Recursive queries, likewise, are no-go for many applications. Want to
hit your database in hundreds of times to generate a single dataset?

This method will assemble all elements and build the complete tree with
a single query, and any element can be examined -- if needed -- for
individual genealogy if desired (the model supports recursion if you
insist on it).

You don't really need a pointer to descendants, since you should build a
SELECT statement based on the element_id as "root", and conduct extra
queries for it as a parent, then descend into those for which they
(themselves are parents, etc.).

Too many pointers and too many resursive techniques are not very
practical on high-volume sites, and large datasets.

Paul Bramscher
Bill Karwin wrote:
Perttu Pulkkinen wrote:
I need to find toplevel image categories and a) number of images
directly in
them and b) number of subcategories directly in those topcategories.

I liked Joe Celko's book "SQL for Smarties", in which he talks about
representing recursive relationships in SQL.

The method he describes is to create another table to record paths in
the "tree":

CREATE TABLE pathenum (
icat_ancestor_id int(11) NOT NULL,
icat_descendant_id int(11) NOT NULL,
pathlength int(11) NOT NULL,
KEY icat_ancestor_id (icat_ancestor_id),
KEY icat_descendant_id (icat_descendant_id),
);

This is instead of recording parent_id in your icategories table.

Populate all paths in the tree, for instance if A -> B -> C, then
you record (A,B,1), (B,C,1), and (A,C,2).

Then you can find toplevel category ids with:

SELECT DISTINCT P.icat_ancestor_id
FROM pathenum P LEFT OUTER JOIN pathenum P2
ON (P.icat_ancestor_id = P2.icat_descendant_id)
WHERE P2.icat_descendant_id IS NULL

You can find all the children of a given toplevel category with:

SELECT P.icat_descendant_id
FROM pathenum P
WHERE P.icat_ancestor_id = ?

Regards,
Bill K.

Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: replace-this-with-my-name | last post by:
Hi. How do I return a string containing an entire menu-tree from a recursive function? Here is my current recursive function: function page_tree( $_i ){ //Call global mysql connection...
19
by: Leif K-Brooks | last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect in Python? I'm getting interested in language implementation, and looking at a reasonably simple example like that could be...
19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
7
by: aurora | last post by:
I love generator and I use it a lot. Lately I've been writing some recursive generator to traverse tree structures. After taking closer look I have some concern on its performance. Let's take...
3
by: Vincenzino | last post by:
Hi, I have some problem in using SQL3 recursive queries on DB2 database system (8.1 and 8.2 UDB). I need to compute the transitive closure of a (possibly) ciclic graph using SQL3 on DB2. I MUST...
6
by: Einar ?rn | last post by:
Hi all, is there a good way to detect recursive C code in large systems? A method or a free tool? Best regards, E
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
2
by: Jim Devenish | last post by:
I have a table named StockItems with field: StockItemID QuantityInStock I am creating assemblies of stock items and have another table named StockItemAssemblies with fields:...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.