[英]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.