Combining `where` and` like` statements with CI activerecords

To shorten the long story: is it possible, and if so - how can I create a query that looks somewhat similar to this

SELECT * FROM a
    WHERE row = 1 AND 
    (other_row LIKE '%..%' OR another_row LIKE '%..%')

Basically, I cannot find a solution to this problem. I just can't figure out how to add parentheses to an activerecords request. Is it possible?

My current code is:

$data = $this->where('row', 1)
    ->like('another_row', '...')        
    ->or_where('row', 1)
    ->like('other_row', $search)                
    ->get('a')
    ->result();

Result:

SELECT * FROM (`a`) WHERE `row` = 1 OR `row` = 1 AND `another_row` LIKE '%...%' AND other_row` LIKE '%...%'
+3
source share
5 answers

You can try this.

   $query = $this->db->select('*')
            ->from('a')
            ->where('row',1)
            ->where("(other_row LIKE '%%' OR another_row LIKE '%%' )")
            ->get();


    foreach ($query->result() as $row) {
        //do sth.
    }

You can write a custom query string (from an active record class)

 Custom string:
 You can write your own clauses manually:

 $where = "name='Joe' AND status='boss' OR status='active'";

 $this->db->where($where);
+6
source

Why aren't you trying to do this:

    $this->db->where("row = 1 AND 
        (other_row LIKE '%..%' OR another_row LIKE '%..%')");
$this->db->get('a');

How can you write custom WHERE in CI. If this is not what you need, feel free to explain

+1
source

or_where, ,

$this->db->query("SELECT * FROM `a` WHERE `row`=1 AND (CONDITION1 OR CONDITION2)")
0
$sql= "SELECT * FROM `a`
WHERE row = '1' AND 
(other_row LIKE '%" . $value . "%' OR another_row LIKE '%" . $value . "%')";
 $this->db->query($sql);
0

:

$this->db->like('title', 'match', 'before');

:

WHERE `title` LIKE '%match' ESCAPE '!'
0
source

All Articles