简体   繁体   English

Zend DB得到明显的数量

[英]Zend DB get Distinct count

hi i have the following result from query: 嗨,我有以下查询结果:

    public function getCityCategory($city_id)
        {
            $select = $this->getDbTable()->getAdapter()->select();
            $select->from('business as b', array('b.cat_id'))
                 ->joinInner('business_category as bc','b.cat_id = bc.cat_id',array('bc.cat_name'))
                 ->where('b.city_id='.$city_id);
$result = $this->getDbTable()->getAdapter()->fetchAll($select);
            return $result;    
        }




Array
(
    [0] => Array
        (
            [cat_id] => 1
            [cat_name] => Restaurants
        )

    [1] => Array
        (
            [cat_id] => 1
            [cat_name] => Restaurants
        )

    [2] => Array
        (
            [cat_id] => 1
            [cat_name] => Restaurants
        )

    [3] => Array
        (
            [cat_id] => 5
            [cat_name] => Arts & Entertainment
        )

    [4] => Array
        (
            [cat_id] => 11
            [cat_name] => Hotels & Travel
        )

    [5] => Array
        (
            [cat_id] => 7
            [cat_name] => Nightlife
        )

    [6] => Array
        (
            [cat_id] => 20
            [cat_name] => Financial Services
        )

    [7] => Array
        (
            [cat_id] => 22
            [cat_name] => Attractions
        )

    [8] => Array
        (
            [cat_id] => 2
            [cat_name] => Active Life
        )

    [9] => Array
        (
            [cat_id] => 22
            [cat_name] => Attractions
        )

    [10] => Array
        (
            [cat_id] => 7
            [cat_name] => Nightlife
        )

    [11] => Array
        (
            [cat_id] => 1
            [cat_name] => Restaurants
        )

    [12] => Array
        (
            [cat_id] => 1
            [cat_name] => Restaurants
        )

)

The query return all category for specific city_id now when i add distinct the above code i get the following result: 查询返回特定city_id所有类别现在,当我添加不同的上述代码时,我得到以下结果:

public function getCityCategory($city_id)
        {

          $select = $this->getDbTable()->getAdapter()->select();
            $select->from('business as b', array('b.cat_id'))
                 ->joinInner('business_category as bc','b.cat_id = bc.cat_id',array('bc.cat_name'))
                 ->distinct()
                 ->where('b.city_id='.$city_id);
   $result = $this->getDbTable()->getAdapter()->fetchAll($select);
            return $result;    
        }

Array
(
    [0] => Array
        (
            [cat_id] => 1
            [cat_name] => Restaurants
        )

    [1] => Array
        (
            [cat_id] => 5
            [cat_name] => Arts & Entertainment
        )

    [2] => Array
        (
            [cat_id] => 11
            [cat_name] => Hotels & Travel
        )

    [3] => Array
        (
            [cat_id] => 7
            [cat_name] => Nightlife
        )

    [4] => Array
        (
            [cat_id] => 20
            [cat_name] => Financial Services
        )

    [5] => Array
        (
            [cat_id] => 22
            [cat_name] => Attractions
        )

    [6] => Array
        (
            [cat_id] => 2
            [cat_name] => Active Life
        )

)

Now the question on how can i add count on last query so i can get total num of business for each category since distinct remove duplicate category? 现在我的问题是如何在最后一个查询中添加计数,这样我就能获得每个类别的总业务量,因为不同的删除重复类别? So the end result should be like this 所以最终的结果应该是这样的

[0] => Array
            (
                [cat_id] => 1
                [cat_name] => Restaurants
                [count] => 5
            ) ....

Thanks 谢谢

Use 采用

 count(cat_name) as count

and then 接着

group by cat_name

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

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