Mysql Left加入DataTable Serverside Codeigniter

时间:2018-05-26 02:00:40

标签: mysql codeigniter datatable left-join

你能帮我解决这个问题吗?

  

没有使用表格

     

SELECT * ORDER BY id asc LIMIT 0

这是我在模型上的sql代码

private function _get_datatables_query()
{

    $this->db->query("SELECT mainproduk.id,
    mainproduk.barcode as barcod,
    mainproduk.nama_produk,
    mainproduk.nama_alias,
    mainproduk.satuan,
    mainproduk.produk_jadi,
    mainproduk.kemasan,
    mainproduk.min_stok_kemasan,
    mainproduk.status,
    mainproduk.top_item,
    mainproduk.tipe_produk,
    mainproduk.nomor_kemtan,
    coalesce(sum(R.jumlah_pc),0) as omzet
    FROM mainproduk
    LEFT JOIN
    (
        SELECT id,barcode, jumlah_pc
        FROM rincian_order WHERE tipe='po' AND status!='canceled' AND tanggal_kirim BETWEEN '$kemarins' AND '$blnkemarin'
    ) AS R
        ON mainproduk.barcode = R.barcode WHERE status=1 GROUP BY mainproduk.id ORDER BY mainproduk.id ASC");


    $i = 0;

    foreach ($this->column_search as $item) // loop column 
    {
        if($_POST['search']['value']) // if datatable send POST for search
        {

            if($i===0) // first loop
            {
                $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                $this->db->like($item, $_POST['search']['value']);
            }
            else
            {
                $this->db->or_like($item, $_POST['search']['value']);
            }

            if(count($this->column_search) - 1 == $i) //last loop
                $this->db->group_end(); //close bracket
        }
        $i++;
    }

    if(isset($_POST['order'])) // here order processing
    {
        $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
    } 
    else if(isset($this->order))
    {
        $order = $this->order;
        $this->db->order_by(key($order), $order[key($order)]);
    }
}

当我使用上述方法时,会出现没有使用表格问题,因为录制已关闭。

然后我用下面的活动记录更改它,并且有一个错误似乎“COALESCE”不支持这种格式,然后如果“coalesce(sum(details_order.jumlah_pc),0)as omzet”我删除它将出现错误,如下所示

private function _get_datatables_query()
{

    $this->db->select('mainproduk.barcode as barcod, mainproduk.nama_produk, mainproduk.nama_alias, mainproduk.satuan, mainproduk.produk_jadi, mainproduk.kemasan, mainproduk.min_stok_kemasan, mainproduk.status, mainproduk.top_item, mainproduk.tipe_produk, mainproduk.nomor_kemtan, coalesce(sum(rincian_order.jumlah_pc),0) as omzet')
        ->from('mainproduk')
        ->join('rincian_order', 'mainproduk.barcode = rincian_order.barcode', 'left')
        ->where('mainproduk.status =', 1)
        ->group_by('mainproduk.id')
        ->order_by('mainproduk.id', 'ASC');     

    $i = 0;

    foreach ($this->column_search as $item) // loop column 
    {
        if($_POST['search']['value']) // if datatable send POST for search
        {

            if($i===0) // first loop
            {
                $this->db->group_start(); // open bracket. query Where with OR clause better with bracket. because maybe can combine with other WHERE with AND.
                $this->db->like($item, $_POST['search']['value']);
            }
            else
            {
                $this->db->or_like($item, $_POST['search']['value']);
            }

            if(count($this->column_search) - 1 == $i) //last loop
                $this->db->group_end(); //close bracket
        }
        $i++;
    }

    if(isset($_POST['order'])) // here order processing
    {
        $this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
    } 
    else if(isset($this->order))
    {
        $order = $this->order;
        $this->db->order_by(key($order), $order[key($order)]);
    }
}
  

order子句中的列'id'是不明确的

请帮忙

1 个答案:

答案 0 :(得分:0)

`mainproduk.id` 
`mainproduk.barcode`

与其他领域相似 尝试将此作为您的语法。

如果这不起作用你可以print_r查询吗?