[英]Codeigniter how to echo more than one INNER JOIN from same table
我是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
这是我的MODEL中的代码示例
模型
$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;
我正在尝试创建一个具有两个团队标志的灯具,但是当我运行此代码时,仅显示一个标志。
可以正确检索所有灯具的详细信息,但是只能检索到一个团队徽标,我需要两个徽标。
我的Model代码中用于检索数据的以下两行相互覆盖,这仅导致第二行工作。
$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"
}
如您所见,我得到了团队徽标之一,而没有另一个。 这是我要解决的问题。 任何帮助,不胜感激。
您要两次加入team
表,这意味着您选择的列名称相同,这就是为什么一列会覆盖另一列值的原因,因为它们的名称相同,因此您应使用新的列名称更改所有t2
别名。 看例子
$this->db->select('tf.*, t1.*, t2.team_id as team_id_2, t2.COL_NAME as COL_NAME_2,......');
这样您将获得t2表的所有不同索引
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.