【问题标题】:get multiple values from same JOIN in MySQL and Codeigniter从 MySQL 和 Codeigniter 中的同一个 JOIN 获取多个值
【发布时间】:2014-02-21 23:28:28
【问题描述】:

这是另一个已部分回答的问题的后续内容,我需要更进一步。

我有一个包含 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 的 JOIN 表,该表具有由 team_id 和 fixture_id 组成的复合 PK

team_has_fixture

team_team_id
fixture_fixture_id
team_team_id2      //added this column to hold the id for second the team in the fixture

我正在尝试创建一个夹具,它使用夹具表中的所有数据和团队表中涉及夹具的两个团队的团队徽标。

例如夹具的布局将如下所示 - 团队 1 徽标 - 夹具详细信息 - 团队 2 徽标

我目前已经到了这个阶段 - 没有团队 2 的徽标

例如夹具的布局将如下所示 - 团队 1 徽标 - 夹具详细信息 -

我想做的是让第二支球队的标志也显示出来。

这是我目前拥有的代码。

型号

 <?php  
 class Fixture_model extends CI_Model {  


public function __construct()
{
  parent::__construct();

}
function fixtures() 

    {
        //$fixture_level = "Roinn 1B";
        //Query the fixture table for every record and row  

        $results = array();

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

        $this->db->join('team', 'team_has_fixture.team_team_id = team.team_id');

         // I tried the line below to get the second logo out 
            but it just overrides the line above and displays the second logo
            instead of the first one but not both 

        //$this->db->join('team as t', 'team_has_fixture.team_team_id2 = t.team_id');
        $this->db->join('fixture', 'team_has_fixture.fixture_fixture_id = fixture.fixture_id');


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

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

        return $results;   

    }

我的视图中有以下代码来显示从数据库中检索到的结果

查看

<?php

     if (is_array($results))
     {
       if( !empty($results) ) 
       {

         foreach($results as $row) 
         {

          echo '<div class="col-sm-6 col-md-6">';
          echo '<div class="thumbnail">';
          echo '<tr>';
          echo '<h4>';
          echo '<td>'.'<img src="'."$row->team_logo".'"> '.$row->fixture_text.'</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>';
         }
       }

      else echo "Not Array";
    }

    ?>

除了我还需要获得第二个团队徽标之外,这一切都有效 - 有什么想法吗??

【问题讨论】:

    标签: mysql sql codeigniter join


    【解决方案1】:

    试试这个! :-)

    $this->db->join('team', 'team_has_fixture.team_team_id = team.team_id AND team_has_fixture.team_team_id2 = t.team_id');
    

    【讨论】:

    • 我目前有这个显示第二个团队徽标 - 它正在覆盖第一个 $this-&gt;db-&gt;select('*'); $this-&gt;db-&gt;from('team_has_fixture as tf'); $this-&gt;db-&gt;join('team as t1', 'tf.team_team_id = t1.team_id'); $this-&gt;db-&gt;join('team as t2', 'tf.team_team_id2 = t2.team_id'); $this-&gt;db-&gt;join('fixture', 'tf.fixture_fixture_id = fixture.fixture_id'); 试图弄清楚如何在 VIEW 中访问它
    • 你试过我的解决方案了吗?您可以将 UNION 与 WHERE 子句一起使用,而不是连接吗?
    • 我尝试了您的解决方案,它在未声明 t.team_id 时返回错误。即 team as t 应该在某处使用
    猜你喜欢
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    相关资源
    最近更新 更多