【问题标题】:Cross join in codeigniter?交叉加入codeigniter?
【发布时间】:2013-03-11 07:42:22
【问题描述】:

我正在尝试这个

$this->db->join('tableTwo as b','','CROSS');
$result = $this->db->get('tableOne as a')->result(); 

一些解决方案?

【问题讨论】:

标签: php codeigniter activerecord


【解决方案1】:

对于那些前来查看 Codeigniter 4 答案的人:

// tableOne is defined in the model
$this->model->join('tableTwo', 'column_x= column_y', 'cross');

第一个参数是表名,下一个参数是连接条件,第三个参数是连接类型。

所以如果你有这样的SQL语句:

cross join producto_resumen_color on prre_id = prco_prre_id

你会这样做:

->join('producto_resumen_color','prre_id = prco_prre_id', 'cross')

您可以在 CI 的用户指南中找到更多信息。

https://codeigniter.com/user_guide/database/query_builder.html?highlight=join#join

join($table, $cond[, $type = ''[, $escape = NULL]])

Parameters: 

    $table (string) – Table name to join
    $cond (string) – The JOIN ON condition
    $type (string) – The JOIN type
    $escape (bool) – Whether to escape values and identifiers

【讨论】:

    【解决方案2】:

    在codeigniter 3中,您可以通过

    添加交叉连接查询生成器
    $this->db->join('tableTwo as b','1=1');                    //true relation
    $result = $this->db->get('tableOne as a')->result(); 
    

    或者通过从方法或获取方法中传入一个数组

    $result = $this->db->get(['tableOne as a','tableTwo as b'])->result(); 
    

    $result = $this->db->from(['tableOne as a','tableTwo as b'])
    get()->result();     //by this method you can add as many table you want to join
    

    或者通过在 from 中传递一个表,在 get 方法中传递另一个表

    $result = $this->db->from('tableOne as a')
    get('tableTwo as b')->result(); 
    

    【讨论】:

      【解决方案3】:

      你应该这样使用它:

      $this->db->join('tableTwo as b','true');
      $result = $this->db->get('tableOne as a')->result(); 
      

      【讨论】:

        【解决方案4】:

        codeigniter 中交叉连接的解决方案:

        $this->db->join('tableTwo as b','true');
        $result = $this->db->get('tableOne as a')->result(); 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-12-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-05-26
          • 1970-01-01
          • 2020-03-15
          • 1970-01-01
          相关资源
          最近更新 更多