[英]Codeigniter Model multiple join statement returns last statement
我有一個聯接語句,該聯接語句從具有TeamID的團隊表中聯接HomeID和AwayID。 當我聯接兩個表時,它僅返回最后一個聯接語句的值。 這是我的模特:-
function get_fixtures(){
$where=array(
'gameweek'=>1,
);
$this->db->select();
$this->db->from('matches AS M');
$this->db->join('team AS T2', 'T2.teamID=M.awayClubID' );
$this->db->join('team AS T1', 'T1.teamID=M.homeClubID');
$this->db->where($where);
$query = $this->db->get();
return $query->result_array();
}
當我打印出結果時,它僅返回T1結果。 如果有人可以幫助將不勝感激:-)
您不能直接返回結果數組
$this->db->select();
$this->db->from('matches M');
$this->db->join('team T2', 'T2.teamID=M.awayClubID' );
$this->db->join('team T1', 'T1.teamID=M.homeClubID');
$this->db->where($where);
$query = $this->db->get();
foreach($query->result_array() as $que)
{
$n[] = $que;
}
retunr $n;
您為同一張表使用2 aliases
。 因此,不要為team
表使用別名。
$this->db->select('*');
$this->db->from('matches AS M');
$this->db->join('team', 'team.teamID=M.awayClubID', 'inner');
$this->db->join('team', 'team.teamID=M.homeClubID', 'inner');
$this->db->where($where);
$query = $this->db->get();
return $query->result_array();
或僅第二次使用它(臨時解決方案)。
$this->db->select('*');
$this->db->from('matches AS M');
$this->db->join('team', 'team.teamID=M.awayClubID', 'inner');
$this->db->join('team as t', 't.teamID=M.homeClubID', 'inner');
$this->db->where($where);
$query = $this->db->get();
return $query->result_array();
編輯:
$this->db->select('*');
$this->db->from('matches AS M');
$this->db->join('team as T', 'T.teamID=M.awayClubID OR T.teamID=M.homeClubID', 'inner');
$this->db->where($where);
$query = $this->db->get();
return $query->result_array();
謝謝大家,我通過區分select語句中的列來弄清楚了。這是代碼:-
function get_fixtures(){
$where=array(
'gameweek'=>1,
);
$this->db->select('m.*, T1.clubName T1name,T2.clubName T2name');
$this->db->from('matches as m');
$this->db->join('team AS T1', 'T1.teamID=M.homeClubID', 'left');
$this->db->join('team AS T2', 'T2.teamID=M.awayClubID', 'left');
$this->db->where($where);
$query = $this->db->get();
return $query->result_array();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.