Good morning All,
I am having an issue with getting a count of records from the adv_table based on the cat_id, when trying to do JOIN so I can get a count of adv_id's from 2 different levels based on cat_id and cat_parent...
to simplify:
adv_table contains records structured as follows:
adv_id / mem_id / cat_id / etc. (most important columns)
cat_table contains records structured as follows:
cat_id / cat_name / cat_parent / etc. (most important columns)
What I need is strictly a count of total records for a single cat_id PLUS
the total records of all cat_id's that have a cat_parent that matches the single cat_id
if cat_id is 1, then I need the count to include all adv_table recs with cat_id=1
PLUS
All adv_table recs with cat_id's where the cat_parent for that cat_id equals 1
I hope that makes sense, and that I'm not confusing the issue.
Anyway, my query consistently comes back with a 1 in every case, both when I know there are multiple records that would be in the intended count, and when there are 0 recs to count.
Here are the related queries that I have. First is a straight count and second is intended to count the ads in the sub categories related to the base category.
This should list the base categories including a count of all ads related to this base category, whether under the base_cat or one of the sub_cats.
$sql="SELECT cat_id, cat_name FROM cat_table where cat_parent='0' ORDER BY cat_name asc";
$basecat = mysql_query($sql) or die($sql);
$cat_count=0;
while($cat_list=mysql_fetch_array($basecat)){
++$cat_count;
$sql_ct = mysql_query("SELECT count(adv_id) FROM adv_table where cat_id='".$cat_list[0]."'");
$basecount = mysql_num_rows($sql_ct);
$sql_ct = mysql_query("SELECT count(a.adv_id) FROM a.adv_table, b.cat_table WHERE a.cat_id=b.cat_id AND b.cat_parent='".$cat_list[0]."'");
$subcount = mysql_num_rows($sql_ct);
$totcount = $basecount + $subcount;
print "<strong>".$cat_list[1]." (".$totcount.")</strong><br />";
}
I'm guessing that my join is messed up, but being a bit limited in knowledge on the use of joins, I can't see what the issue is.
Any suggestions?