【问题标题】:Codeigniter Model multiple join statement returns last statementCodeigniter 模型多连接语句返回最后一条语句
【发布时间】:2014-07-16 13:04:47
【问题描述】:

我有一个连接语句,它从具有 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 结果。如果有人可以提供帮助将不胜感激:-)

【问题讨论】:

  • 尝试使用echo $this->db->last_query();打印sql

标签: php mysql sql codeigniter activerecord


【解决方案1】:

不能直接返回结果数组

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

【讨论】:

  • $query->result_array() 返回一个数组,这不是问题。问题出在连接上。
  • 我认为@ParagTyagi 是对的,问题出在join 语句上。您知道我将如何制定join 语句以便它返回两个join 语句吗?在此先感谢:-)
  • 您不需要在 codeigniter 中使用as。也许这就是问题所在。否则您的查询是正确的。
【解决方案2】:

谢谢大家,我已经通过区分选择语句中的列自己弄清楚了。这是代码:-

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

     }

【讨论】:

    【解决方案3】:

    您正在为同一张表使用 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();
    

    【讨论】:

    • 感谢您的帮助,但我收到此错误 Not unique table/alias: 'team' SELECT * FROM (matches AS M) INNER JOIN team ON team.teamID=@ 987654331@.awayClubID INNER JOIN team ON team.teamID=M.homeClubID WHERE gameweek = 1
    • 和临时解决方案代码仍然只返回 M.homeClubID 而不是 M.awayClubID
    • 检查你是否在这里得到了什么:ellislab.com/forums/viewthread/76383/#384940
    • 帖子来自 2008 年,错误修复链接不起作用。但感谢您的帮助 :-) 将尝试弄清楚...
    • 嘿@bobin56,我在本地尝试过类似的东西并得到了预期的结果。请尝试编辑后的查询。您的解决方案是一种 hack,但不是确切的解决方案。带有or 的简单join 可能会解决您的问题。
    猜你喜欢
    • 2022-01-03
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多