在CodeIgniter上为OR_LIKE查询添加括号

时间:2016-11-23 03:54:12

标签: php mysql codeigniter

我很难在查询中添加括号。我的查询包含ORANDLIKE。这是我的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(); 
}

5 个答案:

答案 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);
}