当前位置: 动力学知识库 > 问答 > 编程问答 >

php - CodeIgniter $this->db->where(); Group Combine ANDs and ORs

问题描述:

I need to create this select:

SELECT * FROM units

WHERE ((um like '%str%' AND desc like '%str%') AND deleted = 0)

so i tried using

$search = 'str';

$this->db->from('units');

$this->db->where("(um LIKE '%".$this->db->escape_like_str($search)."%' or

desc LIKE '%".$this->db->escape_like_str($search)."%') and deleted=0");

return $this->db->get();

but that did not work, even though on my other tables, controllers, modules it works fine.

edit: I saw why it did not work, because of desc... but still my question goes below...

I tried using this and works properly

$this->db->from('units');

$this->db->where('deleted',0);

$this->db->like('desc', $search);

$this->db->or_like('um', $search);

But this formulates as:

WHERE deleted = 0 AND desc like '%str%' AND um like '%str%'

Can I combine or_like and like as 1? and where deleted as another, because now it shows all the deleted because of the or_like.

网友答案:

for this

SELECT * FROM units WHERE ((um like '%str%' AND desc like '%str%') AND deleted = 0)

you have to do:

$this->db->select('*');
$this->db->from('units');
$this->db->where("((units.um LIKE '%str%'  AND units.desc LIKE '%str%') AND deleted = '0')",NULL);
$query = $this->db->get();
return $query->result();
分享给朋友:
您可能感兴趣的文章:
随机阅读: