【问题标题】:How to take count of sub-categories and update parent row count in mysql using single query?如何使用单个查询计算 mysql 中的子类别并更新父行数?
【发布时间】:2011-12-11 17:15:34
【问题描述】:

我在同一张表中存储类别和子类别。如何计算所有子类别并更新我的父类别数据行。

categoryId sub cat id category_count subcategory_count 1 3 0 3 1 4 0 1 1 5 0 7 1 0 5 0

我需要用子类别计数的总和来更新类别计数。

for eg;- 5 is my category count. I want my category count like this 5+ (3+7+1) so my category count should be 16

我怎样才能为此编写一个查询。

请帮帮我 谢谢

【问题讨论】:

  • 如果你更新category_count,下次运行查询时,它会再次将它们全部加起来,所以下次它会变成27,而不是16。这是故意的吗?
  • @learner category_countsubcategory_count 是递归的吗? IE。你如何处理子子类别,子子子类别等......

标签: php mysql sql count


【解决方案1】:

希望这个帮助::

Select (sum(subcatecory_count)+category_count), category_count from table group by category_count;

【讨论】:

  • 嗨 Sashi kant 不使用 category_count 分组可以吗?
  • 你必须明智地获得计数类别,所以你需要分组
猜你喜欢
  • 2012-07-10
  • 2021-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 1970-01-01
相关资源
最近更新 更多