Zend DB得到明显的数量

时间:2013-04-19 09:29:08

标签: php mysql zend-framework

您好我从查询中得到以下结果:

    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
        )

)

查询返回特定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
        )

)

现在关于如何在最后一次查询中添加计数的问题,以便我可以获得每个类别的业务总数,因为不同的删除重复类别?所以最终的结果应该是这样的

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

由于

1 个答案:

答案 0 :(得分:2)

使用

 count(cat_name) as count

然后

group by cat_name