Php Codeigniter模型多连接语句返回最后一条语句
我有一个join语句,它将HomeID和AwayID从具有TeamID的团队表中连接起来。当我连接这两个表时,它只返回最后一个join语句中的值。这是我的模型:-Php Codeigniter模型多连接语句返回最后一条语句,php,mysql,sql,codeigniter,activerecord,Php,Mysql,Sql,Codeigniter,Activerecord,我有一个join语句,它将HomeID和AwayID从具有TeamID的团队表中连接起来。当我连接这两个表时,它只返回最后一个join语句中的值。这是我的模型:- function get_fixtures(){ $where=array( 'gameweek'=>1, ); $this->db->select(); $this->db->from('matches AS M'); $this->db->
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;
您正在为同一个表使用两个别名。因此,不要对团队表使用别名
或者只用于第二次临时解决方案
$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();
编辑:
谢谢大家,我已经通过区分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();
}
$query->result\u数组返回一个数组,没有问题。问题在于join。我认为@ParagTyagi是对的,问题在于join语句。你知道我将如何构造join语句,使其返回两个join语句吗。提前感谢:-你不需要像codeigniter那样。也许这就是问题所在。否则您的查询是正确的。尝试使用echo$this->db->last\u查询打印sql;感谢您的帮助,但我收到了以下错误:非唯一表/别名:“team”选择*从匹配中作为M Internal JOIN team ON team.teamID=M.awayClubID Internal JOIN team ON team.teamID=M.homeClubID,其中gameweek=1和临时解决方案代码仍然只返回M.homeClubID,而不是M.awayClubIDcheck,如果您在此处获得了一些信息:这篇文章是从2008年开始的,错误修复链接不起作用。但是感谢你的帮助tho:-将尝试找出t…嘿@bobin56,我在我的本地网站上尝试了类似的东西,并得到了预期的结果。请尝试编辑的查询。你的解决方案有点像黑客,但不是确切的解决方案。使用or的简单连接可能会解决您的问题。
$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();
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();
}