Search code examples
phpmysqlwhere-clausesql-like

Like and Where in the same MySQL query


I would like to use something like the following:

SELECT city FROM cities WHERE city LIKE %D% AND country_id = '12'

Solution

  • You need to quote the string

    SELECT city FROM cities WHERE city LIKE '%D%' AND country_id = '12'
    

    But remember that using a LIKE with a pattern starting with a "%" means the server will NOT use an index on 'city' column - it may not matter in your specific case but something to be aware of. Here's the reference since your comment indicates you're not familiar with indexes.