【问题标题】:Join tables and keep all rows from main table连接表并保留主表中的所有行
【发布时间】:2019-07-05 23:50:27
【问题描述】:

如何在保留主表中的所有行的同时加入表?
具体使用 CodeIgniter 的数据库库。

例如,以下代码仅返回来自table1table2 的连接行,其中条件适用于两个表。
如何从table1 返回所有 行,并仅在条件适用的情况下连接来自table2 的行?

$this->db->join('table2', 'table2.col = table1.col');
$query = $this->db->get('table1');

【问题讨论】:

    标签: php mysql codeigniter join


    【解决方案1】:

    LEFT JOIN 子句包括“左”表中的所有行(在本例中为table1)。

    在 CodeIgniter 中,这是通过将其作为第三个参数添加到 join 方法中来完成的:

    $this->db->join('table2', 'table2.col = table1.col', 'left');
    $query = $this->db->get('table1');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 2020-02-01
      • 2017-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多