2

I have a problem with mysql select using "not like" in where conditions. There are 15k records in table. 3k records are with value 'test' in column col1.

This select works fine:

select 
    *
from
    `table`
where
    `col1` like 'test'

3000 rows selected. This is correct.

But if I try this select:

select
   *
from
   `table`
where
   `col1` not like 'test'

0 rows selected while I expect 12000 rows.

I will be grateful for any ideas?

6
  • 1
    Can you make a small example at sqlfiddle? Commented Jan 23, 2014 at 11:17
  • not sure but try two different querry with like col1 like '%test%' and col1 NOT like '%test%' Commented Jan 23, 2014 at 11:18
  • Works fine for me: sqlfiddle.com/#!2/59eb8e/3 Commented Jan 23, 2014 at 11:19
  • It works fine on sqlfiddle but still not working in my DB. Commented Jan 23, 2014 at 11:42
  • sqlfiddle.com/#!2/66080/3 Commented Jan 23, 2014 at 11:42

7 Answers 7

5

So I solved it. Problem was in data, not in request. I didn't realize that not like condition doesn't work for NULL value.

http://sqlfiddle.com/#!2/17270/2

Sign up to request clarification or add additional context in comments.

1 Comment

I build a quick turnaround : select ... where COALESCE(field, "****") not like "test". This will include null values aswell.
2

Try this

select * from table where col1 NOT LIKE '%test%';

1 Comment

If he's doing an exact match with LIKE, why shouldn't he do an exact match with NOT LIKE?
2

If you are making exact comparison as you are doing in query, you can use NOT EQUAL

select
   *
from
   `table`
where
   `col1` != 'test'

1 Comment

Thanks for idea but it didn't work. The same result of 0 rows.
1

So I solved my problem with:

where ifnull(column,'') not like 'value'

1 Comment

When answering an old question, your answer would be much more useful to other StackOverflow users if you included some context to explain how your answer helps, particularly for a question that already has an accepted answer. See: How do I write a good answer.
0

try this

SHOW TABLES WHERE col1 NOT LIKE '%test%'

Comments

0

It might be lack of %

select
   *
from
   `table`
where
   `col1` not like '%test%'

Try without `

select
   *
from
   table
where
   col1 not like '%test%'

2 Comments

The 2nd solution will not compile.
You're request will not use any index a it will explore every single row in table. It will be extremly slow.
0

I was doing a query similar to:

select col from my_table where not like 'some_text%'

and it returned no results when I expected some.

It turns out the underscore needs escaping

select col from my_table where not like 'some\_text%'

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.