views:

52

answers:

1

I'am not sure that while problem, but take a look:

I have 3 tables:

  1. Categories: id, icon, name;
  2. Subcategories: id, name, description, category_id;
  3. Topics: id, title, text, author, created, subcategory_id;

Now I'm trying get this info(query):

  $String = "SELECT
  categories.id AS catid,
  categories.icon AS caticon,
  categories.name AS catname,
  subcategories.id AS scatid,
  subcategories.name AS scatname,
  subcategories.description AS scatdescription,
  subcategories.category_id AS scatcatid,
  COUNT(topics.id) AS tid,
  topics.title AS ttitle,
  topics.author AS tauthor,
  topics.created AS tcreated
       FROM
  categories
LEFT JOIN
  subcategories
ON
  subcategories.category_id = 1
LEFT JOIN
  topics
ON
  subcategories.id = topics.subcategory_id
GROUP BY
 categories.id";

Result:

Categories from 5 showing 5 - OK, Subcategories from 4 showing only 1 in first categories.

Maybe the query is too long? Thanks for any answer.

+2  A: 

this

LEFT JOIN
  subcategories
ON
  subcategories.category_id = 1

should be this

LEFT JOIN
  subcategories
ON
  subcategories.category_id = categories.id
Nicolas78
well spotted! I just blinked, went WTF? and left...
Daren Thomas
Still not working.
user100246
do you get the same results, or not working in a new way?
Nicolas78
Yes, get same result. only 1 subcategory in categories.
user100246
$string_result = mysql_query($string_query)or die(mysql_error());while($string_row = mysql_fetch_array($string_result)){Maybe here wrong ?
user100246
looks fine (except neither result nor row are strings, so if you want to put the type in the name, it's the wrong one. not related to functioning though in any way)maybe try it a little easier first - what happens if you leave out the second join on the topics?
Nicolas78