我有一个join语句,它从具有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结果。如果有人可以提供帮助将会欣赏: - )
答案 0 :(得分:0)
您无法直接返回结果数组
$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;
答案 1 :(得分:0)
您在同一张桌子上使用 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();
OR 仅第二次使用它(临时解决方案)。
$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();
答案 2 :(得分:0)
谢谢大家,我通过区分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();
}