简体   繁体   English

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

[英]Codeigniter Model multiple join statement returns last statement

I have a join statement which joins HomeID and AwayID from a team table that has TeamID. 我有一个联接语句,该联接语句从具有TeamID的团队表中联接HomeID和AwayID。 When I join the two tables it only returns the values from the last join statement. 当我联接两个表时,它仅返回最后一个联接语句的值。 Here is my model :- 这是我的模特:-

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

When I print out the result it only returns T1 results. 当我打印出结果时,它仅返回T1结果。 If anyone can help will appreciate :-) 如果有人可以帮助将不胜感激:-)

You cant return the result array directly 您不能直接返回结果数组

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

You are using 2 aliases for the same table. 您为同一张表使用2 aliases Hence don't use alias for team table. 因此,不要为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 only use it for the second time (temp solution). 仅第二次使用它(临时解决方案)。

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


EDIT: 编辑:

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

Thanks guys,i have figure it out myself by distinguishing the columns in the select statement.Here is the code :- 谢谢大家,我通过区分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.

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