简体   繁体   English

联接三表 codeigniter 4

[英]Join three table codeigniter 4

Hi everyone i'am new on codeigniter 4 and currently working on a small project in the project i'am trying to join three tables and display there data in single table.大家好,我是 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 |
------------------------

I've tried to join with, but not working.我试过加入,但没有工作。

        $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();

Is there another way to Join them?还有其他方法可以加入他们吗? Thank you谢谢

What you currently have won't work because your Table_1 and Table_2 are effectively the same table.您当前所拥有的将无法使用,因为您的 Table_1 和 Table_2 实际上是同一张表。

Taking your Attempt and correcting it to use LEFT JOIN尝试并更正它以使用 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;
}

You would get...你会得到...

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"
  }
}

Note that you have, two occurrences of name in your query.请注意,您的查询中有两次出现的名称。 So which one will win?那么哪一个会赢呢? It appears that Table_2.name is only used and any reference to Table_1.name is NULL as it's not used.似乎只使用了 Table_2.name 并且对 Table_1.name 的任何引用都是 NULL 因为它没有被使用。

You could give them different names using aliases but then you would have something like name_1 and name_2 so which one is it?您可以使用别名给它们起不同的名称,但是您会得到类似 name_1 和 name_2 的名称,那么它是哪一个? This is due to the duplication in your Table_1 and Table_2 and you asking for both.这是由于您的 Table_1 和 Table_2 中的重复,并且您要求两者。

The Better way So in this case you would need to perform an UNION on Table_1 and Table_2.更好的方法所以在这种情况下,您需要对 Table_1 和 Table_2 执行 UNION。

I don't think that there is a UNION command in the CI query builder.我认为 CI 查询生成器中没有 UNION 命令。

Using mysql, it would be...使用 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();
}

So in this example, we have specified the 3 fields you require in the Select.所以在这个例子中,我们在 Select 中指定了您需要的 3 个字段。 Note the T12.name is renamed author.注意 T12.name 被重命名为作者。 (See the output below) (见下面的 output)

Then an UNION has to be performed on Table_1 and Table_2 and the result is named (aliased) as T12 (shorthand for Table_1 and Table_2) as the result requires a new name.然后必须对 Table_1 和 Table_2 执行UNION ,并将结果命名(别名)为 T12(Table_1 和 Table_2 的简写),因为结果需要一个新名称。

Then a LEFT JOIN is performed against Table_3, which will give all combinations where there will be NULLS, so the WHERE statement filters those out using "IS NOT NULL" on Table_3.author.然后对 Table_3 执行LEFT JOIN ,这将给出所有存在 NULL 的组合,因此 WHERE 语句使用 Table_3.author 上的“IS NOT NULL”过滤掉这些组合。

I left out the ORDER BY as it's not really needed and you can add that back in if you wish to.我省略了ORDER BY ,因为它并不是真正需要的,如果您愿意,可以将其添加回来。

A var_dump() of the result gives...结果的 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"
  }
}

So that will give you the id,author and title for each matching row as you have requested using your example Tables.因此,这将为您提供每个匹配行的 ID、作者和标题,正如您使用示例表所请求的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM