简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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