Codeigniter Active Record'where_or'分组

时间:2012-01-05 18:57:53

标签: mysql codeigniter activerecord

是否可以将 group 和_where / or_where / or_like ...语句放在一起,以免与其他和/ where语句混合使用。

会导致

的东西
  

WHERE city ='My City'AND state ='My State'AND(name LIKE%name1%   或者命名LIKE%name2%)

3 个答案:

答案 0 :(得分:16)

我知道这有点晚了,但我在查看它时找到了这篇文章,并找到了比Sean提出的解决方案更好的解决方案。

$name = 'Bob';

$this->db->where('city', 'My City');
$this->db->where('state', 'My State');
$this->db->group_start();
    $this->db->like('name', $name);
    $this->db->or_like('name', $name);
$this->db->group_end();

答案 1 :(得分:12)

是。 $this->db->where()允许您手动编写子句。你可以这样做:

$this->db->where('city', 'My City');
$this->db->where('state', 'My State');
$this->db->where('(name LIKE %name1% OR name LIKE %name2%)', null, false);

将第三个参数设置为false可防止CodeIgniter尝试向该子句添加反引号。

查看Active Record documentation。标题为 $ this-> db-> where(); 的部分解释了可用于设置 WHERE 子句的四种方法。

答案 2 :(得分:1)

这两项功能group_end()$name1 = 'Bob'; $name2 = 'John'; $this->db->where('city', 'My City'); $this->db->where('state', 'My State'); $this->db->group_start(); $this->db->like('name', $name1); $this->db->or_like(('name', $name2); $this->db->group_end(); 已在 CodeIgniter 3.0.3 中实施。根据原始问题,这里有更好的实现。

name
相关问题