Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: NOT LIKE

Tags:

mysql

sql-like

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.

like image 955
Run Avatar asked Mar 18 '11 00:03

Run


People also ask

Can you use not like in MySQL?

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.

Can you do a not like in SQL?

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.

What is the opposite of like in MySQL?

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 .


2 Answers

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%' 
like image 119
Dalen Avatar answered Oct 12 '22 22:10

Dalen


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') 
like image 27
Piotr Salaciak Avatar answered Oct 12 '22 23:10

Piotr Salaciak