这是我的模特
function report($where = '')
{
$this->db->select(array('o.id_order AS id_order', 'nama_pemesan', 'kota', 'total', 'SUM(biaya) AS do.biaya'));
$this->db->from('t_order o JOIN t_detail_order do ON (o.id_order = do.id_order)');
$this->db->where($where);
$this->db->group_by('o.id_order');
return $this->db->get();
}
这是我的表t_order
这是我的表t_detail_order
这是错误
这是我的控制器报告
公共职能报告() { $这 - >负载>库(' form_validation&#39); $这 - > cek_login();
if ($this->input->post('submit', TRUE) == 'Submit') { $this->form_validation->set_rules('bln', 'Bulan', 'required|numeric'); $this->form_validation->set_rules('thn', 'Tahun', 'required|numeric'); if ($this->form_validation->run() == TRUE) { $bln = $this->input->post('bln', TRUE); $thn = $this->input->post('thn', TRUE); } } else { $bln = date('m'); $thn = date('Y'); } //YYYY-mm-dd //2017-04-31 $awal = $thn.'-'.$bln.'-01'; $akhir = $thn.'-'.$bln.'-31'; $where = ['tgl_pesan >=' => $awal, 'tgl_pesan <=' => $akhir, 'status_proses' => 'proses', 'selesai']; $data['data'] = $this->trans->report($where); $data['bln'] = $bln; $data['thn'] = $thn; $this->template->admin('admin/laporan', $data); }
答案 0 :(得分:1)
加入:
$this->db->join('t_detail_order do', 'o.id_order = do.id_order');
结果:
$query = $this->db->get();
CI查询构建器文档: https://www.codeigniter.com/userguide3/database/query_builder.html
答案 1 :(得分:0)
将id_order
中的t_detail_order
更改为varchar(15)
。清除所有数据并重试
答案 2 :(得分:0)
尝试此操作会对您有所帮助: -
function report($where = ''){
$this->db->select(array('o.id_order AS id_order', 'o.nama_pemesan','o.kota', 'o.total', 'SUM(do.biaya) AS biaya'));
$this->db->from('t_order o');
$this->db->join('t_detail_order do', 'o.id_order = do.id_order');
$this->db->where($where);
$this->db->group_by('o.id_order');
return $this->db->get();
}
答案 3 :(得分:-1)
我不会使用查询生成器,而是使用它。
function report($where = '')
{
$sql = "SELECT o.id_order AS id_order, nama_pemesan, kota, total,
SUM(do.biaya) AS biaya
FROM t_order o
JOIN t_detail_order AS do ON (o.id_order=do.id_order) WHERE ?
GROUP BY o.id_order";
return $this->db->query($sql, $where)->get();
}
检查htat report()
的返回值是否为FALSE,如果查询失败则返回该值。在控制器中
$data - $this->model_name->report($some_value);
if($data)
{
... do stuff with data
}
else
{
... report on, or handle query failure
}