Combining `where` and `like` statements by using t

2020-03-05 09:01发布

问题:

To cut a long story short: Is it possible and if it is - how can I build a query that looks somewhat like this one

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

Basically I cann't come up / find a solution to this problem. I just cann't seem to figure how to add the brackets to the activerecords query. Is that even possible?

My current code:

$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 '%...%'

回答1:

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 custom query string (from 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);


回答2:

I also having problem with or_where so i simply make my custom query like

$this->db->query("SELECT * FROM `a` WHERE `row`=1 AND (CONDITION1 OR CONDITION2)")


回答3:

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


回答4:

Why dont you try this:

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

It's how you can write custom WHERE in CI. If it is not what u r looking for, feel free to explain



回答5:

You can use the following:

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

It will produce:

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


回答6:

$this->db->where('salary_range_min >= ',$salarymin)
$this->db->where('salary_range_max <= ',$salarymax)
$this->db->where('job_title like '.$title.'% or skill like %'.$title.'%');
// OR */
$this->db->where('job_title like '.$title.'% or skill like %'.$title.'%',FALSE);
// OR */
$this->db->where('job_title like '.$title.'% or skill like %'.$title.'%',NULL);

Try this out