php – 在CodeIgniter上为OR_LIKE查询添加括号

前端之家收集整理的这篇文章主要介绍了php – 在CodeIgniter上为OR_LIKE查询添加括号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我很难在我的查询添加括号.我的查询包括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(); 
}
最佳答案
我不认为纯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语句.

原文链接:https://www.f2er.com/mysql/433033.html

猜你在找的MySQL相关文章