簡體   English   中英

Codeigniter如何從同一表中回顯多個INNER JOIN

[英]Codeigniter how to echo multiple INNER JOIN from same table

我有兩個表movieDirector ,在電影表中我有兩個字段:' director1 '和' director2 '。

使用內部聯接,我匹配了兩個表,但是當我對“ director”的字段“ name”和“ surname”進行foreach時,我只有一個結果,最后一個。

這是我的模型:

function getLastMoviesId($id)
    {
        $this->db->select('*');
        $this->db->from('movie as m');
        $this->db->join('director as d1', 'd1.id = m.director1');
        $this->db->join('director as d2', 'd2.id = m.director2');
        $this->db->where('m.id', $id);
        $query = $this->db->get();
        if($query->num_rows() > 0){
          return $query->result();
        } else {
          return 0;
        }   
    }

這是我的看法:

<?php foreach ($archivepage as $m): ?>
  <?= html_entity_decode($m->name) ?>
  <?= html_entity_decode($m->surname) ?>
<?php endforeach ?>

您的字段值被覆蓋,因為它包含相同的名稱,請改用類似的名稱。

$this->db->select('m.*, d1.name d1name, d1.surname d1surname, d2.name d2name, d2.surname d2surname')
$this->db->from('movie as m');
$this->db->join('director as d1', 'd1.id = m.director1', 'left');
$this->db->join('director as d2', 'd2.id = m.director2', 'left');
$this->db->where('m.id', $id);

在您的視圖中,現在您可以訪問像這樣的值

對於第一個

->d1name ->d1surname 

第二個

->d2name ->d2surname 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM