Skip to main content

[ask]CASE query dalam codeigniter

December 9, 2010 by dewwe

dewwe's picture

guys, mau tanya nih. gw punya query di mysql..dapat berjalan baik dan hasilnya sesuai dengan keinginan.

select 
dt_memo.nota_id,
ms_division.division_name,
dt_memo.subject,
dt_memo.content,
dt_memo.`to`,
dt_memo.cc, dt_memo.created_data_approved,
ms_employee.name as pengirim
from
dt_memo, ms_division, ms_employee
where 
	case when dt_memo.`to`=2 then dt_memo.`to` 
		when dt_memo.`to`!=2 and dt_memo.cc=2 then dt_memo.`to` end 
	and
	dt_memo.`to`=ms_division.division_id
	and 
	ms_employee.npp = dt_memo.`from`
	and
	dt_memo.status_id = 5
	and 
	dt_memo.status_approve = 'sudah2'
group by 
dt_memo.nota_id

Lalu, gw coba implementasi dalam codeigniter. sebagai berikut

$this->db->select("dt_memo.nota_id,
								ms_division.division_name,
								dt_memo.subject,
								dt_memo.`to`,
								dt_memo.cc, dt_memo.created_data_approved,
								ms_employee.name");
			$this->db->from("dt_memo, ms_division, ms_employee");
			$this->db->where("CASE 
							 	WHEN dt_memo.to = '$divisi_id_user' THEN dt_memo.to
								WHEN dt_memo.to!= '$divisi_id_user' AND dt_memo.cc = '$divisi_id_user' then dt_memo.to
							  END");
			$this->db->limit($num, $offset);
			$this->db->group_by('dt_memo.nota_id');
			$this->db->order_by('dt_memo.created_data_approved', 'asc');
			return $this->db->get()->result();

tetapi, saat aplikasi dijalankan, keluar error message seperti ini

A Database Error Occurred
 
Error Number: 1064
 
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHEN dt_memo.to = '2' THEN dt_memo.to WHEN dt_memo.to!= '2' AND dt_memo.cc = '' at line 4

apakah CASE query tidak dapat diimplementasikan dalam codeigniter?

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

re: case

December 9, 2010 by dewwe, 2 weeks 5 days ago
Comment: 7463

dewwe's picture

yap. sudah berhasil mas..terima kasih :)

fi

December 14, 2010 by firman_id, 1 week 6 days ago
Comment: 7469

firman_id's picture

gmna mas caranya??

BLS:

December 9, 2010 by syabac, 2 weeks 5 days ago
Comment: 7461

syabac's picture

coba pas bagian where, ubah dikit jadi

->where('case ....... end', null, false)

Premium Drupal Themes by Adaptivethemes