Search code examples
phpmysqlcodeigniteractiverecordsql-like

How to create MULTIPLE LIKE query for the same column in codeigniter?


Let's say I want to get all rows in a text column that contain both of three words (word1, word2, word3)

so my sql would be

SELECT 
    *
FROM
    mytable
WHERE
    col_text LIKE '%word1%'
        AND col_text LIKE '%word2%'
        AND col_text LIKE '%word3%'

is there any and_like in codeigniter?


Solution

  • Supplying three like() to the query builder will assume, by default AND for each:

    $this->db
         ->from( "mytable" )
         ->like( "col_text", 'word1', 'both' )
         ->like( "col_text", 'word2', 'both' )
         ->like( "col_text", 'word3', 'both' );