簡體   English   中英

codeigniter join 2 表數據

[英]codeigniter join 2 table data

大家好,我是 codeigniter 的新手,目前正在項目中的一個小項目中工作,我正在嘗試連接兩個表並在單個表中顯示那里的數據。 我查看了 codeigniter 的用戶指南,我不確定這是如何工作的

$this->db->join();

哪個表應該是第一個,什么 id 鍵應該是第一個。 如果可以的話,有人可以更詳細地解釋我嗎,請使用示例。 我正在嘗試加入憑證表和 tblanswers。 Tnx 回答。

我試圖使用這個例子編寫一個函數:

$this->db->select('*');
$this->db->from('blogs');
$this->db->join('comments', 'comments.id = blogs.id');

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

編輯:是否可以使用簡單的函數來分別檢索兩個表數據,而不是在 codeigniter 中使用 join 方法? 我想要的只是將數據庫表中的數據回顯到我網站頁面中要顯示的 html 表中是否可以編寫兩個 get 函數來分別檢索兩個表?

首先是什么表並不重要......簡單地說:

<?php

$this->db->select('t1.name, t2.something, t3.another')
     ->from('table1 as t1')
     ->where('t1.id', $id)
     ->join('table2 as t2', 't1.id = t2.id', 'LEFT')
     ->join('table3 as t3', 't1.id = t3.id', 'LEFT')
     ->get();
?>

下面是它的工作原理:

  1. 假設我們有兩個表,分別是學生、圖書館。

注意:但請記住,如果您想使用 where 條件 / 此處我們假設兩個表都有std_id列,則其中一列應該匹配

  1. 如下編寫select查詢,在括號中寫下你想要的所有東西

注意:按如下所示寫,不要在每個單獨的地方加上引號,一次把它放在一個整體上。

*注意:假設我們想要姓名,電話號碼。 來自學生表和book_name表單庫表。*

      $this->db->select('name, phone_number, book_name');
  1. 現在編寫 from 查詢並編寫表名之一(無規則)

     $this->db->from('student');
  2. 現在用連接查詢將它與另一個表連接起來

     $this->db->join('library', 'students.std_id=library_std_id');
  3. 現在寫出你想要書名形式庫表 where std id=1 的 where 條件(實際上你需要從視圖/數據庫中獲取這個 id)

     $this->db->where('std_id', 1); $q= $this->db->get();

就是這樣,現在您可以打印並檢查結果了。

$this->db->join('comments', 'comments.id = blogs.id');

用這一行告訴你:在評論中搜索我所有 id 等於 blogs.id 的評論。

通常是這樣的,我認為:

$this->db->join('comments', 'comments.blogs_id = blogs.id');

您必須在表中插入一個名為 blogs_id(int value unisgned)的字段,因為博客可以有更多評論。 第一個或第二個值的位置不重要

嗨,這將適用於在 codeIgnator 中連接兩個表。

$this->db->select("chat.id,chat.name,chat.email,chat.phone,chat.date,post.post");
      $this->db->from('chat');
      $this->db->join('post', 'chat.id = post.id');
      $query = $this->db->get();

    if($query->num_rows() != 0)
    {
        return $query->result();
    }
    else
    {
        return false;
    }

您可以隨心所欲地更改查詢,執行跟蹤和錯誤方法以獲得適當的結果。

這是我盡可能連接多個表的代碼。

我有 3 桌professeurspublicationssupport

public function toutesdonnées(){

    $this->db->select("*");
      $this->db->from('publication');
      $this->db->join('support', 'publication.idsup = support.idsup');
      $this->db->join('professeurs', 'publication.emailprof = professeurs.emailprof');
      $query = $this->db->get();

    if($query->num_rows() != 0)
    {
        return $query->result();
    }
    else
    {
        return false;
    }

暫無
暫無

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

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