0

嗨,我有以下查询结果:

    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
            ) ....

谢谢

4

1 回答 1

2

利用

 count(cat_name) as count

接着

group by cat_name
于 2013-04-19T09:45:23.943 回答