简体   繁体   English

计算菜单项返回的行数

[英]Count number of returned rows for menu items

i have 2 tables: Categories,Oglasi 我有2张桌子:类别,Oglasi

categories
category_id | category | parent
1             auto      0
2             games     0
3             bmw       1
4             cards     2

oglasi
oglas_id | category_id
1          3            
2          4             

What im trying to do is make a menu of this as: 我正在尝试做的是将菜单设置为:

parent name - category name ( number of items in this category) 父级名称-类别名称(此类别中的项目数)

Example : auto - bmw ( 1 ) 例如:auto-bmw(1)

Now i got it to make a tree menu of parent name-category name,but don't know how to connect all of this with the counted rows number. 现在我得到它来制作父名称-类别名称的树菜单,但是不知道如何将所有这些与计数的行号连接起来。 Here's my query and code to do this: 这是我的查询和执行此操作的代码:

$kategorije = dbQuerySelect('SELECT a.category parent
                                  , b.category child
                                  FROM categories a
                                  JOIN categories b
                                  ON a.category_id = b.parent
                                  ORDER BY a.category_id');
                if ($kategorije)
                  {
                    $parent = '';
                    echo "<ul>";
                    foreach ($kategorije as $next) {
                       if ($next['parent'] != $parent) {
                          if (strlen($parent) > 0) {
                             echo "    </ul>";
                             echo "  </li>";
                          }
                          echo "  <li>" . $next['parent'];
                          echo "    <ul>";
                       }
                       echo "    <li>" . $next['child'] . "</li>";

                       $parent = $next['parent'];
                    }
                    echo "    </ul>";
                    echo "  </li>";
                    echo "</ul>";
                  }

So in this case this returns: 因此,在这种情况下,将返回:

parent | child
auto     bmw
games    cards

Can someone please help me to edit my code and return the number of rows in each 'subcategory' ? 有人可以帮我编辑代码并返回每个“子类别”中的行数吗? I tried a few variations with COUNT in the query,but i just couldn't make it work. 我在查询中尝试使用COUNT进行一些变体,但是我无法使其工作。

SELECT a.category parent, 
       b.category child,
       (select count(*) from oglasi where category_id = b.category_id) as count
FROM categories a
JOIN categories b
ON a.category_id = b.parent
ORDER BY a.category_id

With this you should get: 有了这个你应该得到:

   parent|child|count
   auto  bmw    1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM