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.
MySQL NOT LIKE is used to exclude those rows which are matching the criterion followed by LIKE operator. Pattern matching using SQL simple regular expression comparison. Returns 1 (TRUE) or 0 (FALSE). If either expr or pat is NULL, the result is NULL.
The NOT LIKE operator in SQL is used on a column which is of type varchar . Usually, it is used with % which is used to represent any string value, including the null character \0 . The string we pass on to this operator is not case-sensitive.
In MySQL, you can use NOT LIKE to perform a negation of the LIKE operator. In other words, NOT LIKE returns the opposite result to LIKE .
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%'
I don't know why
cfg_name_unique NOT LIKE '%categories%'
still returns those two values, but maybe exclude them explicit:
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%' AND developer_configurations_cms.cfg_name_unique NOT IN ('categories_posts', 'categories_news')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With