我很难在查询中添加括号。我的查询包含OR
,AND
和LIKE
。这是我的MySQL查询:
SELECT * FROM (`vendor`) JOIN `vendor_detail` ON `vendor`.`id_vendor`
= `vendor_detail`.`id_vendor` JOIN `klasifikasi` ON `vendor_detail`.`id_klasifikasi` = `klasifikasi`.`id_klasifikasi`
WHERE `vendor_detail`.`id_klasifikasi` = 'K0002' AND
`vendor`.`st_aktif` = 1 AND (`vendor`.`id_vendor` LIKE '%eka%' OR
`vendor`.`nama_vendor` LIKE '%eka%' OR `vendor`.`alamat` LIKE '%eka%'
OR `vendor`.`telp` LIKE '%eka%' OR `vendor`.`mobile` LIKE '%eka%' OR
`vendor`.`fax` LIKE '%eka%' OR `vendor`.`cp` LIKE '%eka%' OR
`vendor`.`telp_cp` LIKE '%eka%' OR `vendor`.`catatan` LIKE '%eka%' OR
`vendor`.`email` LIKE '%eka%')
如您所见,我在vendor.st_aktif = 1 AND
之后和我查询的最后一行添加了括号
如何实现上述查询到Code Igniter Active Records?
这是我目前的CI AR:
function src_vendor_by_name($src_nama,$src_kls){
$this->db->select('*');
$this->db->from('vendor');
$this->db->join('vendor_detail', 'vendor.id_vendor = vendor_detail.id_vendor');
$this->db->join('klasifikasi', 'vendor_detail.id_klasifikasi = klasifikasi.id_klasifikasi');
$this->db->where('vendor_detail.id_dklasifikasi', $src_kls);
$this->db->where('vendor.st_aktif', 1);
if($src_nama != "")
{
$this->db->or_like('vendor.id_vendor',$src_nama);
$this->db->or_like('vendor.nama_vendor',$src_nama);
$this->db->or_like('vendor.alamat',$src_nama);
$this->db->or_like('vendor.telp',$src_nama);
$this->db->or_like('vendor.mobile',$src_nama);
$this->db->or_like('vendor.fax',$src_nama);
$this->db->or_like('vendor.cp',$src_nama);
$this->db->or_like('vendor.telp_cp',$src_nama);
$this->db->or_like('vendor.catatan',$src_nama);
$this->db->or_like('vendor.email',$src_nama);
}
return $this->db->get()->result_array();
}
答案 0 :(得分:6)
Query grouping允许您通过将它们括在括号中来创建WHERE子句组。这将允许您使用复杂的WHERE子句创建查询。
<强> $这 - &GT; DB-&GT; group_start()强>
通过在查询的WHERE子句中添加左括号来启动新组。
<强> $这 - &GT; DB-&GT; group_end()强>
通过向查询的WHERE子句添加右括号来结束当前组。
function src_vendor_by_name($src_nama,$src_kls){
$this->db->select('*');
$this->db->from('vendor');
$this->db->join('vendor_detail', 'vendor.id_vendor = vendor_detail.id_vendor');
$this->db->join('klasifikasi', 'vendor_detail.id_klasifikasi = klasifikasi.id_klasifikasi');
$this->db->where('vendor_detail.id_dklasifikasi', $src_kls);
$this->db->where('vendor.st_aktif', 1);
if($src_nama != "")
{
$this->db->group_start();
$this->db->or_like('vendor.id_vendor',$src_nama);
$this->db->or_like('vendor.nama_vendor',$src_nama);
$this->db->or_like('vendor.alamat',$src_nama);
$this->db->or_like('vendor.telp',$src_nama);
$this->db->or_like('vendor.mobile',$src_nama);
$this->db->or_like('vendor.fax',$src_nama);
$this->db->or_like('vendor.cp',$src_nama);
$this->db->or_like('vendor.telp_cp',$src_nama);
$this->db->or_like('vendor.catatan',$src_nama);
$this->db->or_like('vendor.email',$src_nama);
$this->db->group_end();
}
return $this->db->get()->result_array();
}
答案 1 :(得分:4)
我不认为纯CodeIgniter会允许您按照您想要的方式执行嵌套WHERE
子句。但一种选择是使用自定义字符串:
$where = "(`vendor`.`id_vendor` LIKE '%eka%' OR ";
$where .= "`vendor`.`nama_vendor` LIKE '%eka%' OR";
$where .= "`vendor`.`alamat` LIKE '%eka%' OR ";
$where .= "`vendor`.`telp` LIKE '%eka%' OR ";
$where .= "`vendor`.`mobile` LIKE '%eka%' OR ";
$where .= "`vendor`.`fax` LIKE '%eka%' OR ";
$where .= "`vendor`.`cp` LIKE '%eka%' OR ";
$where .= "`vendor`.`telp_cp` LIKE '%eka%' OR ";
$where .= "`vendor`.`catatan` LIKE '%eka%' OR ";
$where .= "`vendor`.`email` LIKE '%eka%')";
$this->db->where($where);
有些名为IgnitedQuery的内容似乎支持嵌套的WHERE
语句。
答案 2 :(得分:3)
只需使用:
$这 - &GT; DB-&GT; group_start()
$这 - &GT; DB-&GT; group_end()
答案 3 :(得分:2)
答案 4 :(得分:0)
首先检查条件'%eka%'(Var)
If('%eka%' != ''){
$where = "(`vendor`.`id_vendor` LIKE '%eka%' OR `vendor`.`nama_vendor` LIKE '%eka%' OR `vendor`.`alamat` LIKE '%eka%' OR `vendor`.`telp` LIKE '%eka%' OR `vendor`.`mobile` LIKE '%eka%' OR `vendor`.`fax` LIKE '%eka%' OR `vendor`.`cp` LIKE '%eka%' OR `vendor`.`telp_cp` LIKE '%eka%' OR `vendor`.`catatan` LIKE '%eka%' OR `vendor`.`email` LIKE '%eka%')";
$this->db->where($where);
}