简体   繁体   中英

mysql Error Code : 1052 with Zend_Db

i have 2 tables that are

1 = "issue_history"

2 = "issue_history_status"

"issue_history_id" is column name which is in both tables.

PK in "issue_history" & FK in "issue_history_status"

this is my query

$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');

but its giving me the error

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

here is my full code

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();

}

COUNT(issue_history_id)

That field is in both tables hence the ambiguity . You need to also mention table name for it to work. Which issue_history_id you want to count

COUNT(ih.issue_history_id)

Or

COUNT(ihs.issue_history_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM