Search code examples
mysqlcodeignitersql-like

mysql like query with special character '


I wrote query for filter data using name and wrote following query

SELECT * FROM (`abc`) WHERE (name LIKE "%test\'!&@#$\%-(3)\_er%")

It should return records which has name start with text "test" but it will not instead of if I modify query like

SELECT * FROM (`abc`) WHERE (name LIKE "%test%\'!&@#$\%-(3)\_er%")

then it will give result. Why it is not give result with first query? Is there any other way to do this?


Solution

  • The % is the wildcard in the query.
    So %test means everything that ends with test.
    and test% means everything that begins with test.
    and %test% means everything with test in it.

    Simpy change your query to

    SELECT * FROM (abc) WHERE (name LIKE "test%")