2017-05-17 5 views
-3

Hier bekommen, ist mein Code i die Anzahl der männlichen bekommen müssen und weiblich getrenntWie kann ich die männlichen und weiblichen Zählung Zählung getrennt

public function getmaletofemaleCountById() 
{ 
    $select = $this->select(); 
    $select->from($this->_name, array('count(*) AS candidateSum','count(candidate_gender='.MALE.') AS male','count(candidate_gender='.FEMALE.') AS female')); 
    $select->joinLeft(array('batch' => DBPREFIX.'batch'), 'batch.batch_training_center=tc_id AND batch.deleted=0', array()); 
    $select->joinLeft(array('candidate' => DBPREFIX.'candidate'), 'candidate.candidate_batch_id=batch.batch_id AND candidate.deleted=0', array()); 
    $select->where('tc_id = ?', $this->tc_id); 
    $select->where('candidate_gender != ?', 3); 
    //$select->group('candidate_gender'); 
    die($select); 

    return $this->fetchRow($select); 
} 
+1

SELECT COUNT (CASE WHEN candidate_gender = 'MALE' THEN id END) AS Männchen , COUNT (FALL WENN candidate_gender = 'FEMALE' THEN id END) AS Frauen, COUNT (*) AS Gesamt FROM your_table; –

Antwort

1
$select->from($this->_name, array('count(*) AS candidateSum','SUM(IF(candidate_gender='.MALE.',1,0)) AS male','SUM(IF(candidate_gender='.FEMALE.',1,0)) AS female')); 

Sie haben Ihr Array mit diesen zu aktualisieren. SUM() ist hier für männliche und weibliche beide bedingt.

So sollte Ihre aktualisierte Code wie unten sein:

public function getmaletofemaleCountById(){ 
      $select=$this->select(); 
      $select->from($this->_name, array('count(*) AS candidateSum','SUM(IF(candidate_gender='.MALE.',1,0)) AS male','SUM(IF(candidate_gender='.FEMALE.',1,0)) AS female')); 
      $select->joinLeft(array('batch'=>DBPREFIX.'batch'), 'batch.batch_training_center=tc_id AND batch.deleted=0', array()); 
      $select->joinLeft(array('candidate'=>DBPREFIX.'candidate'), 'candidate.candidate_batch_id=batch.batch_id AND candidate.deleted=0', array()); 
      $select->where('tc_id = ?',$this->tc_id); 
      $select->where('candidate_gender != ?',3); 
      //$select->group('candidate_gender'); 
      die($select); 

      return $this->fetchRow($select); 
} 
1

unten angegebenen Code funktioniert für mich

public function getmaletofemaleCountById() 
    { 
     $select=$this->select(); 
     $select->from($this->_name, array('count(candidate.candidate_batch_id) AS candidateSum','SUM(case when candidate_gender='.MALE.' then 1 else 0 end) as maleCount' ,'SUM(case when candidate_gender='.FEMALE.'then 1 else 0 end) as femaleCount','SUM(case when candidate_gender='.TRANSGENDER.'then 1 else 0 end) as transCount')); 
     $select->joinLeft(array('batch'=>DBPREFIX.'batch'), 'batch.batch_training_center=tc_id AND batch.deleted=0', array()); 
     $select->joinLeft(array('candidate'=>DBPREFIX.'candidate'), 'candidate.candidate_batch_id=batch.batch_id AND candidate.deleted=0', array()); 
     $select->where('tc_id = ?',$this->tc_id); 

     return $this->fetchRow($select); 
     }