Codeigniter DB Query with where, or, and.. and like

17,551

Did you try this?

SELECT * FROM (`nyb_articles`) 
WHERE `show_year` = '2009' AND `idn` = 'the' 
OR `show_year`='2009' AND `post_slug` LIKE '%the%'

I think the following produces expression above:

$this->db->select(...)->from(...)->
where("show_year","2009")->where("idn","the")->
or_where("show_year","2009")->like("post_slug","%the%")
Share:
17,551
chris
Author by

chris

nothing to report currently..

Updated on June 26, 2022

Comments

  • chris
    chris almost 2 years

    I have a query the conditions of it below (minus select, from, etc)

    $this->db->where('show_year', $yyyy);
    $this->db->or_where('idn', $idn);
    $this->db->or_like('post_slug', $idn);
    

    which forms

    SELECT * FROM (`nyb_articles`) 
    WHERE `show_year` = '2009' 
    OR `idn` = 'the' 
    AND `post_slug` LIKE '%the%'
    

    However I am looking to have it be more like

    SELECT * FROM (`nyb_articles`) 
    WHERE `show_year` = '2009' 
    AND (`idn` = 'the' OR `post_slug` LIKE '%the%')
    

    My problem is Im not sure if CI's active record supports this notion, and if it does, how would I tackle it, as I can't find notion of it anywhere in the docs. No matter how I change up the like, or_like, where, or_where I can't even nail something similar. So anyone got any ideas? I'd prefer to stay away from a raw query if at all possible, but if I have to prepare one and do it that way, I will, just prefer not to.