【问题标题】:Codeigniter how to echo more than one INNER JOIN from same tableCodeigniter 如何从同一个表中回显多个 INNER JOIN
【发布时间】:2014-02-22 12:23:17
【问题描述】:

我是 Codeigniter 的新手,过去三天我一直在做这件事,但我已经走到了死胡同。我正在运行以下查询以从表中获取夹具详细信息。

我有两个表 team 和 fixture,它们有一个多对多的关系,导致一个名为 team_has _fixture 的 Join 表

下面是我的表格

 team

 team_id
 team_name
 team_logo


 fixture

 fixture_id
 fixture_text
 fixture_comp
 fixture_type
 fixture_level
 fixture_date


 team_has_fixture

 team_team_id
 team_fixture_id
 team_team_id2

这是我的模型中的代码示例

型号

$results = array();

$this->db->select('*');
$this->db->from('team_has_fixture as tf');

$this->db->join('team as t1', 'tf.team_team_id = t1.team_id');
$this->db->join('team as t2', 'tf.team_team_id2 = t2.team_id');
$this->db->join('fixture', 'tf.fixture_fixture_id = fixture.fixture_id');

$query = $this->db->get();

   if($query->num_rows() > 0) 
     {
      $results = $query->result();
     }

        return $results; 

我正在尝试创建一个夹具,其中包含两个团队的徽标,但是当我运行此代码时,只显示一个徽标。

正确检索了所有夹具详细信息,但仅检索了一个团队徽标,并且我需要两个徽标。

我的模型代码中用于检索数据的以下两行相互覆盖,导致只有第二行工作。

 $this->db->join('team as t1', 'tf.team_team_id = t1.team_id');
 $this->db->join('team as t2', 'tf.team_team_id2 = t2.team_id');

如果我注释掉第二行,第一行会带回赛程和第一支球队标志的所有细节。如果我不注释掉第二行,查询会带回所有夹具详细信息和第二支球队的标志——它似乎覆盖了上面的行

我已经在此处和其他资源中搜索了解决方案,我真的坚持这一点,希望能提供任何帮助。

我也不确定如何要求在我的视图中显示第二个徽标。

在我的视图中显示结果的代码是

查看

<?php

     if (is_array($results))
     {
       if( !empty($results) ) 
       {
        foreach($results as $row) 
       {

          echo '<div class="col-sm-12 col-md-12">';
          echo '<div class="thumbnail">';

          echo '<tr>';
          echo '<h4>';
          echo '<td>'.$row->fixture_type.'</td>'."</br></br>";

          echo '<td>'.'<img src="'."$row->team_logo".'">  '.$row->fixture_text.'   <img src="'."$row->team_logo".'"> '.'</td>'."</br></br>";

          echo '<td>'.$row->fixture_level.'</td>'."</br></br>";
          echo '<td>'.$row->fixture_comp.'</td>'."</br>";
          echo '</h4>';

          echo '<td>'.$row->fixture_date.'</td>'."</br></br>";
          echo '</tr>';
          echo '</div>';
          echo '</div>';
         }
       }

使用 echo $this->db->last_query();显示我的最后一个查询,我得到以下内容

SELECT * FROM (`team_has_fixture` as tf) JOIN `team` as t1 ON `t1`.`team_id` =  `tf`.`team_team_id` JOIN `fixture` ON `tf`.`fixture_fixture_id` = `fixture`.`fixture_id`

使用 var_dump($query->result());显示我的查询结果我得到以下内容

[0]=> object(stdClass)#22 (15) 

{ 

["team_team_id"]=> string(1) "5" 
["fixture_fixture_id"]=> string(2) "62" 
["team_team_id2"]=> string(2) "15" 
["team_id"]=> string(2) "15" 
["team_name"]=> string(8) "Kilkenny" 
["team_logo"]=> string(25) "../../crests/kilkenny.png" 
["fixture_id"]=> string(2) "62" 
["fixture_text"]=> string(16) "Clare V Kilkenny" 
["fixture_type"]=> string(7) "Hurling" 
["fixture_comp"]=> string(36) "Allianz Hurling League 2014 Roinn 1A" 
["fixture_round"]=> string(7) "Round 1" 
["fixture_level"]=> string(8) "Roinn 1A" 
["fixture_time"]=> string(3) "2pm" 
["fixture_date"]=> string(26) "Sunday February 16th, 2014" 
["fixture_venue_id"]=> string(1) "5" 

}

如您所见,我得到了其中一个队徽,但没有得到另一个。这是我试图解决的问题。非常感谢任何帮助。

【问题讨论】:

  • $query = $this-&gt;db-&gt;get(); 之后添加echo $this-&gt;db-&gt;last_query(); 并在您的问题中添加查询。

标签: php sql codeigniter codeigniter-2 inner-join


【解决方案1】:

您要加入 team 表两次,这意味着您的选择具有相同的列名,这就是为什么一列会覆盖另一列值,因为它们的名称相同,因此您应该使用新列更改所有 t2 别名姓名。看例子

$this->db->select('tf.*, t1.*, t2.team_id as team_id_2, t2.COL_NAME as COL_NAME_2,......');

因此您将获得 t2 表的所有不同索引

【讨论】:

  • 我已经尝试过了,它给出了一个语法错误。 $this-&gt;db-&gt;select('tf.*, t1.*, t2.team_id as team_id_2, t2.team_name as t2.team_name_2, t2.team_logo as t2.team_logo_2');
  • $this-&gt;db-&gt;select('tf.*, t1.*, t2.team_id as team_id_2, t2.team_name as team_name_2, t2.team_logo as team_logo_2'); 不是 t2.team_logo_2
  • 当你说t2.COL_NAME as SHOULD_BE_A_NAME_ONLY 时你不能说t2.NEW_NAME 因为 NEW_NAME 不在 t2 中,但是你给了一个新的列名,我错误地在我的例子中添加了 t2
  • Minhaz Ahmed 非常感谢 - 你已经解决了 :) 非常感谢你的帮助
猜你喜欢
  • 1970-01-01
  • 2012-03-11
  • 2018-03-07
  • 1970-01-01
  • 2023-03-10
  • 2012-08-10
  • 1970-01-01
  • 2018-11-17
  • 2011-11-18
相关资源
最近更新 更多