0

我有 2 张桌子

1 =“问题历史”

2 =“问题历史状态”

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

“issue_history”中的 PK 和“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();

}
4

1 回答 1

1

COUNT(issue_history_id)

该字段在两个表中,因此存在歧义。您还需要提及表名才能使其正常工作。您要计算哪个 issue_history_id

COUNT(ih.issue_history_id)

或者

COUNT(ihs.issue_history_id)
于 2013-04-01T08:11:14.577 回答