3

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

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

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

我当前的代码:

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

6 回答 6

5

你可以试试这个。

   $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);
于 2012-05-08T05:40:11.810 回答
0

我也有问题,or_where所以我只是让我的自定义查询像

$this->db->query("SELECT * FROM `a` WHERE `row`=1 AND (CONDITION1 OR CONDITION2)")
于 2012-05-08T05:26:07.323 回答
0
$sql= "SELECT * FROM `a`
WHERE row = '1' AND 
(other_row LIKE '%" . $value . "%' OR another_row LIKE '%" . $value . "%')";
 $this->db->query($sql);
于 2012-05-08T05:26:53.493 回答
0

你为什么不试试这个:

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

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

于 2012-05-08T05:44:16.553 回答
0

您可以使用以下内容:

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

它将产生:

WHERE `title` LIKE '%match' ESCAPE '!'
于 2017-07-13T07:11:36.453 回答
0
$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);

试试这个

于 2019-01-04T10:36:07.193 回答