简体   繁体   中英

sql query with like and or combination

I need to do something like this sql query but in codeigniter:

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

I tried to do whit this code:

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

But the result in sql was:

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

Thanks

There are no parenthesis in CI v2. You'll need to use your where() function like:

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

The null is just a placeholder in this case; the TRUE keeps from adding ` (backticks) whihc are usually just in the way here. You can see more in the Users Guide

$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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM