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