【问题标题】:I need to join two tables using datatables of codeignitor我需要使用 codeigniter 中的数据表连接两个表
【发布时间】:2021-04-05 21:52:58
【问题描述】:

我试过了

$this->datatables
     ->select($this->db->dbprefix('companies').".id as id, company as sname", false)
     ->from("companies")
     ->join("(select finance, name as sname from payment) d", 'd.finance = companies.finance','left')
     ->where("category_id",$parent_id);

但输出只显示公司名称,我需要在名称列中同时显示公司名称和付款名称。

【问题讨论】:

    标签: php sql codeigniter datatables


    【解决方案1】:

    试试这个:

    $query = $this->db->select('*')
                ->from('companies')
                ->join('payment d', 'd.finance = companies.finance', 'left');
                ->where('category_id', $parent_id);
                ->get();
            
    return $query->result();
    

    【讨论】:

      【解决方案2】:

      更简单的方法是“SQL查询”而不是ORM:

      $params = array();
      $sql =   "select * from companies c 
                  left join payment   d 
                    on d.finance=c.finance 
                 where category_id = ?";
      
      array_push($params, $parent_id);
      $result = $this->db->query($sql, $params)->result_array();
      return $result;
      

      【讨论】:

        猜你喜欢
        • 2023-03-24
        • 1970-01-01
        • 1970-01-01
        • 2016-11-18
        • 1970-01-01
        • 2017-02-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多