2014-01-23 50 views
3

我有一個mysql選擇使用「不喜歡」在哪裏條件的問題。表中有15k條記錄。 3k條記錄在col1欄中的值爲'test'。mysql不喜歡不行不通

這個選擇優良工程:選擇

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

3000行。這是對的。

但如果我試試這個選擇:選擇

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

0行,而我預計12000行。

我會感激任何想法嗎?

+1

你可以在sqlfiddle做一個小例子嗎? – Barmar

+0

不確定,但嘗試兩個不同的querry,像'col1 like'%test%''和'col1不是'%test%'' –

+0

適合我使用:http://www.sqlfiddle.com/#!2/ 59eb8e/3 – Barmar

回答

2

如果您正在爲您查詢做精確的比較,你可以使用NOT EQUAL

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

感謝您的想法,但它沒有奏效。 0行的結果相同。 – sajushko

2

試試這個

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

如果他正在與'LIKE'完全匹配,爲什麼他不應該與'NOT LIKE'完全匹配? – Barmar

+0

謝謝,但這並沒有工作。 – sajushko

0

試試這個

SHOW TABLES WHERE col1 NOT LIKE '%test%' 
0

這可能是缺乏%

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

嘗試沒有`

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

第二個解決方案不會編譯。 –

+0

你的請求將不會使用任何索引,它將探索表中的每一行。這將非常緩慢。 – sajushko