mysql错误代码:1052与Zend_Db

时间:2013-04-01 08:08:16

标签: mysql zend-framework join zend-db

我有2张

1 =“issue_history”

2 =“issue_history_status”

“issue_history_id”是两个表中的列名。

PK在“issue_history”& “issue_history_status”中的FK

这是我的查询

$sql = $this->_db->select()
    ->from(array('ih'  => 'issue_history'),array('COUNT(issue_history_id) as tot','DATE(date_created)AS date'))
    ->join(array('ihs' => 'issue_history_status'),'ih.issue_history_id = ihs.issue_history_id');

但它给了我错误

Error Code : 1052 Column 'issue_history_id' in field list is ambiguous

这是我的完整代码

public function getDataForGraph($sd,$ed,$issues,$os,$pf,$model,$version){

      $sql = $this->_db->select()
             ->from(array('ih'  => 'issue_history'),array('COUNT(issue_history_id) as tot','DATE(date_created)AS date','issue_history_id'))
             ->join(array('ihs' => 'issue_history_status'),'ih.issue_history_id = ihs.issue_history_id');



  if(Engine::notEmpty($sd) && $sd != 'sd' && SG_Support_Engine::notEmpty($ed) && $ed != 'ed'){
                $sql->where('DATE(ih.date_created) >= ?',$sd );
                $sql->where('DATE(ih.date_created) <= ?',$ed );
            }
  if(Engine::notEmpty($issues) && $issues != 'issues'){
               $issues = implode(',', $issues);
               $sql->where('ihs.issue_id IN('.$issues.')');
           }

 if(Engine::notEmpty($os) && $os != 'os'){
                $sql->where('ih.phone_service_os IN (?)',$os );
            }

  if(Engine::notEmpty($pf) && $pf != 'pf'){
                $sql->where('ih.phone_service_device IN (?)',$pf );
            }

    if(Engine::notEmpty($model) && $model != 'model'){
                $sql->where('ih.phone_service_model IN (?)',$model );
            }

    if(Engine::notEmpty($version) && $version != 'ver'){
               $sql->where('ih.phone_service_version IN (?)',$version );
            }

          $sql->group('DATE(ih.date_created)');
          $sql->order('DATE(ih.date_created)');

          echo $sql->__toString(); exit;

           return $sql->query()->fetchAll();

}

1 个答案:

答案 0 :(得分:1)

COUNT(issue_history_id)

该字段在两个表中都是模糊的。您还需要提及表名称才能使用。您想要计算哪个issue_history_id

COUNT(ih.issue_history_id)

或者

COUNT(ihs.issue_history_id)