2011-05-19 48 views
1

嗨,我試圖讓行的計數,但至今沒有運氣,也許有什麼東西與數據庫becouse我得到一些奇怪的數字如何計算行zend框架?

這裏的數據庫EGS

poll_id option_poll_id gender 
1  100   male 
1  100   male 
1  103   femeale 
1  103   male 

所以我想是對於每個選項100和103來獲得男性或女性的數量

像這樣選項100-> 2男性和選項103-> 1男性 在此先感謝。

回答

2

你的查詢是什麼樣子的?這應該得到你所需要的數字:

SELECT option_poll_id, gender, COUNT(gender) 
FROM yourtable 
GROUP BY option_poll_id, gender 
5

下面是直接從http://framework.zend.com/manual/en/zend.db.table.html

$table = new Bugs(); 

    $select = $table->select(); 

    $select->from($table, 

       array('COUNT(reported_by) as `count`', 'reported_by')) 

     ->where('bug_status = ?', 'NEW') 

     ->group('reported_by'); 

    $rows = $table->fetchAll($select); 

採取的例子同樣適用於這裏。改變你的表名和字段:)。

您還可以執行@Marc B的查詢。這對Zend_Db_Table更具體。

+1

感謝大家的意見,但是我仍然有一個小問題 – 2011-05-20 05:44:57

+0

不知道你在面對什麼我們怎麼知道問題:( – 2011-05-20 06:26:19

2

這是我現在使用的代碼但是結果是好的,但是當我獲取它們它顯示像

Engine_Db_Table_Rowset Object ([_rowClass:protected] => Engine_Db_Table_Row [_rowsetClass:protected] => Engine_Db_Table_Rowset [_data:protected] => Array ([0] => Array ([poll_option_id] => 108 [gender] => male [count] => 1)) [_table:protected] => Poll_Model_DbTable_Votes Object ([_rowClass:protected] 


foreach ($this->pollOptions as $i => $option_chart_f): 

$table = Engine_Api::_()->poll()->api()->getDbtable('votes', 'poll'); 
    $select = $table->select() 
        ->from($table->info('name'), array(
         'poll_option_id','gender', 
         new Zend_Db_Expr('COUNT(gender) as count'), 
        )) 
        ->where('poll_id = ?', $option_chart_f->poll_id) 
        ->where('poll_option_id =?', $option_chart_f->poll_option_id) 
        ->group('poll_option_id') 
        ->group('gender'); 

$rows = $table->fetchAll($select); 
print_r($rows); 

endforeach; 

東西謝謝