[英]Zend DB get Distinct count
嗨,我有以下查詢結果:
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
) ....
謝謝
采用
count(cat_name) as count
接着
group by cat_name
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.