Codeigniter模型多连接语句返回最后一个语句

时间:2014-07-16 13:04:48

标签: php mysql sql codeigniter activerecord

我有一个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结果。如果有人可以提供帮助将会欣赏: - )

3 个答案:

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

     }