通过使用 CI 活动记录组合“位置”和“喜欢”语句


Combining `where` and `like` statements by using the CI activerecords

长话短说:是否可能,如果是 - 我如何构建一个看起来有点像这个的查询

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

基本上我无法提出/找到解决这个问题的方法。我似乎无法弄清楚如何将括号添加到活动记录查询中。这可能吗?

我当前的代码:

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

结果:

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

你可以试试这个。

   $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.
    }

您可以编写自定义查询字符串(从活动记录类(

 Custom string:
 You can write your own clauses manually:
 $where = "name='Joe' AND status='boss' OR status='active'";
 $this->db->where($where);

我也遇到了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);

你为什么不试试这个:

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

这就是在 CI 中编写自定义 WHERE 的方式。如果这不是您要找的,请随时解释

您可以使用

以下内容:

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

它将产生:

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

试试这个