简体   繁体   English

使用CI activerecords组合`where`和`like`语句

[英]Combining `where` and `like` statements by using the CI activerecords

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. 我似乎无法想象如何将括号添加到activerecords查询。 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 '%...%'

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

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. 这是你如何在CI中编写自定义WHERE。 If it is not what ur looking for, feel free to explain 如果不是您想要的,请随时解释

我也有or_where问题,所以我只是简单地使我的自定义查询

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

You can use the following: 您可以使用以下内容:

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

It will produce: 它会产生:

WHERE `title` LIKE '%match' ESCAPE '!'
$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 试试吧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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