使用like和or组合的SQL查询

时间:2013-11-02 22:22:16

标签: sql codeigniter where combinations

我需要做类似这样的SQL查询,但需要在codeigniter中执行:

SELECT `table`.* FROM (`table`) WHERE `name` LIKE '%9%' OR `id` LIKE '%9%' AND (`name` LIKE '%f%' OR `id` LIKE '%f%')

我尝试使用此代码:

$this->db->select('table.*');
$this->db->from('table');
foreach($data as $d){
    $this->db->like("name", $d)
    $this->db->or_like("id", $d);
}

但是sql中的结果是:

SELECT `table`.* FROM (`table`) WHERE `name` LIKE '%9%' OR `id` LIKE '%9%' AND `name` LIKE '%f%' OR `id` LIKE '%f%'

由于

2 个答案:

答案 0 :(得分:0)

CI v2中没有括号。您需要使用where()函数,如:

 ->where('mysql in here', null, true);

在这种情况下,null只是一个占位符; TRUE不会添加`(反引号)通常只是在这里。您可以在“用户指南”中查看更多内容

答案 1 :(得分:0)

$this->db->select('*');
$this->db->from('tablename');
$this->db->like('name','9','both');
$this->db->or_like('id','9','both');
$this->db->like('name','f','both');
$this->db->or_like('id','f','both');