Codeigniter parentheses in dynamic Active Record query

后端 未结 8 1582
死守一世寂寞
死守一世寂寞 2020-12-05 23:38

I\'m producing a query like the following using ActiveRecord

SELECT * FROM (`foods`) WHERE `type` = \'fruits\' AND 
       `tags` LIKE \'%green%\' OR `tags`          


        
8条回答
  •  执念已碎
    2020-12-06 00:12

    In the CI 3.0-dev you can add groups in query:

    $this->db->select('id, title')
    ->group_start()
    ->or_like([ 'title' => $s, 'id' => $s ])
    ->group_end()                
    ->where([ 'b_del' => 0 ]);
    

    Produces:

    SELECT `id`, `title`, `venda1`
    FROM `itens`
    WHERE   
    (
    `title` LIKE '%a%' ESCAPE '!'
    OR  `id` LIKE '%a%' ESCAPE '!'
     )
    AND `b_del` =0 
    

提交回复
热议问题