MySQL查询,加入我的问题?

时间:2010-07-27 15:18:31

标签: mysql

我不确定问题,但看看:

我有3张桌子:

  1. 类别:id,icon,name;
  2. 子类别:id,name,description,category_id;
  3. 主题:id,title,text,author,created,subcategory_id;
  4. 现在我正在尝试获取此信息(查询):

      $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";
    

    结果:

    5分类显示5 - 好的,   4个子类别在第一类中仅显示1个。

    也许查询太长了?谢谢你的回答。

1 个答案:

答案 0 :(得分:2)

LEFT JOIN
  subcategories
ON
  subcategories.category_id = 1

应该是这个

LEFT JOIN
  subcategories
ON
  subcategories.category_id = categories.id