簡體   English   中英

聯接三表 codeigniter 4

[英]Join three table codeigniter 4

大家好,我是 codeigniter 4 的新手,目前正在項目中的一個小項目上工作,我正在嘗試加入三個表並在單個表中顯示數據。

Table_1
id unique_id Name
1  1111      Sam   
2  2222      Charlote

Table_2
id unique_id Name
1  1212      Jhon 
2  5151      Alex

Table_3
id author title
1  1111   Book_1
2  5151   Book_2
3  1111   Book_3


Result
------------------------
| No | Author | Title  |
------------------------
| 1  | Sam    | Book_1 |
| 2  | Alex   | Book_2 |
| 3  | Sam    | Book_3 |
------------------------

我試過加入,但沒有工作。

        $this->join('Table_1', 'Table_1.unique_id = Table_3.author ');
        $this->join('Table_2', 'Table_2.unique_id = Table_3.author ');
        $this->select('Table_1.Name');
        $this->select('Table_2.Name');
        $this->select('Table_3.*');
        $this->orderBy('Table_3.id');
        return  $this->findAll();

還有其他方法可以加入他們嗎? 謝謝

您當前所擁有的將無法使用,因為您的 Table_1 和 Table_2 實際上是同一張表。

嘗試並更正它以使用 LEFT JOIN

public function example_1() {
    $this->join('Table_1', 'Table_1.unique_id = Table_3.author', 'LEFT');
    $this->join('Table_2', 'Table_2.unique_id = Table_3.author', 'LEFT');
    $this->select('Table_1.Name');
    $this->select('Table_2.Name');
    $this->select('Table_3.*');
    $this->orderBy('Table_3.id');
    $result = $this->findAll();

    echo $this->db->getLastQuery();

    return $result;
}

你會得到...

SELECT `Table_1`.`Name`, `Table_2`.`Name`, `Table_3`.*
FROM `Table_3`
LEFT JOIN `Table_1` ON `Table_1`.`unique_id` = `Table_3`.`author`
LEFT JOIN `Table_2` ON `Table_2`.`unique_id` = `Table_3`.`author`
ORDER BY `Table_3`.`id`

array(3) {
  [0]=>
  array(4) {
    ["Name"]=>
    NULL
    ["id"]=>
    string(1) "1"
    ["author"]=>
    string(4) "1111"
    ["title"]=>
    string(6) "Book_1"
  }
  [1]=>
  array(4) {
    ["Name"]=>
    string(4) "Alex"
    ["id"]=>
    string(1) "2"
    ["author"]=>
    string(4) "5151"
    ["title"]=>
    string(6) "Book_2"
  }
  [2]=>
  array(4) {
    ["Name"]=>
    NULL
    ["id"]=>
    string(1) "3"
    ["author"]=>
    string(4) "1111"
    ["title"]=>
    string(6) "Book_3"
  }
}

請注意,您的查詢中有兩次出現的名稱。 那么哪一個會贏呢? 似乎只使用了 Table_2.name 並且對 Table_1.name 的任何引用都是 NULL 因為它沒有被使用。

您可以使用別名給它們起不同的名稱,但是您會得到類似 name_1 和 name_2 的名稱,那么它是哪一個? 這是由於您的 Table_1 和 Table_2 中的重復,並且您要求兩者。

更好的方法所以在這種情況下,您需要對 Table_1 和 Table_2 執行 UNION。

我認為 CI 查詢生成器中沒有 UNION 命令。

使用 mysql,它將是...

public function get_book_and_author() {
    $sql = "SELECT Table_3.id, T12.name as author, Table_3.title 
            FROM (
                SELECT Table_1.* FROM Table_1
            UNION
                SELECT Table_2.* FROM Table_2
                ) as T12
            LEFT JOIN Table_3 ON T12.unique_id = Table_3.author  
            WHERE Table_3.author IS NOT NULL
            ";
    $result = $this->db->query($sql);

    return $result->getResultArray();
}

所以在這個例子中,我們在 Select 中指定了您需要的 3 個字段。 注意 T12.name 被重命名為作者。 (見下面的 output)

然后必須對 Table_1 和 Table_2 執行UNION ,並將結果命名(別名)為 T12(Table_1 和 Table_2 的簡寫),因為結果需要一個新名稱。

然后對 Table_3 執行LEFT JOIN ,這將給出所有存在 NULL 的組合,因此 WHERE 語句使用 Table_3.author 上的“IS NOT NULL”過濾掉這些組合。

我省略了ORDER BY ,因為它並不是真正需要的,如果您願意,可以將其添加回來。

結果的 var_dump() 給出了...

array(3) {
  [0]=>
  array(3) {
    ["id"]=>
    string(1) "1"
    ["author"]=>
    string(3) "Sam"
    ["title"]=>
    string(6) "Book_1"
  }
  [1]=>
  array(3) {
    ["id"]=>
    string(1) "2"
    ["author"]=>
    string(4) "Alex"
    ["title"]=>
    string(6) "Book_2"
  }
  [2]=>
  array(3) {
    ["id"]=>
    string(1) "3"
    ["author"]=>
    string(3) "Sam"
    ["title"]=>
    string(6) "Book_3"
  }
}

因此,這將為您提供每個匹配行的 ID、作者和標題,正如您使用示例表所請求的那樣。

暫無
暫無

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

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