MySQL: NOT LIKE

laukok picture laukok · Mar 18, 2011 · Viewed 279.6k times · Source

I have these text in my db,

categories_posts
categories_news
posts_add
news_add

And I don't want to select the rows with categories, I use a query something like this,

SELECT *
    FROM developer_configurations_cms

    WHERE developer_configurations_cms.cat_id = '1'
    AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
    AND developer_configurations_cms.cfg_name_unique NOT LIKE '%categories%'

but it returns these two in the output as well...

categories_posts
categories_news

How can I ignore them in my query?

Thanks.

Answer

Dalen picture Dalen · Mar 18, 2011

categories_posts and categories_news start with substring 'categories_' then it is enough to check that developer_configurations_cms.cfg_name_unique starts with 'categories' instead of check if it contains the given substring. Translating all that into a query:

SELECT *
    FROM developer_configurations_cms

    WHERE developer_configurations_cms.cat_id = '1'
    AND developer_configurations_cms.cfg_variables LIKE '%parent_id=2%'
    AND developer_configurations_cms.cfg_name_unique NOT LIKE 'categories%'