I have the following table:
categories:
cat_id | cat_cat | cat_name
1 | 0 | name1
2 | 0 | name2
3 | 1 | name3
4 | 2 | name4
5 | 2 | name5
6 | 0 | name6
7 | 1 | name7
I am trying to select all the categories with cat_cat=0, and selecting a count of every record that has cat_cat as the current cat_id, essentially giving me the following result:
cat_id | cat_cat | cat_name | cat_subcats
1 | 0 | name1 | 2
2 | 0 | name2 | 2
6 | 0 | name7 | 0
I have tried multiple queries and my current one is as follows:
SELECT i.* , COUNT( i2.icon_id ) AS subcats
FROM themes_icons i, themes_icons i2
WHERE i.icon_cat = '0'
AND i2.icon_cat = i.icon_id
GROUP BY i.icon_cat, i2.icon_id
ORDER BY i.icon_order ASC
This query seems to give me the following results:
cat_id | cat_cat | cat_name | cat_subcats
1 | 0 | name1 | 1
1 | 0 | name1 | 1
2 | 0 | name2 | 1
2 | 0 | name2 | 1
6 | 0 | name7 | 0
Anyone know how I can get the desired results. I’ve tried join statements too, but they keep giving me back errors.
Thanks in advance.
Its a standard
SELECT, except the subselect that counts the subcategories for the current category.