views:

69

answers:

2
>> Comment.count
  SQL (0.3ms)   SELECT count(*) AS count_all FROM `comments` 
=> 451
>> Comment.count(:conditions => ["author_website not like ?",'aaaa'])
  SQL (1.4ms)   SELECT count(*) AS count_all FROM `comments` WHERE (author_website not like 'aaaa') 
=> 203
>> Comment.count(:conditions => ["author_website like ?",'aaaa'])
  SQL (1.2ms)   SELECT count(*) AS count_all FROM `comments` WHERE (author_website like 'aaaa') 
=> 0
>>

I was expecting the count of NOT LIKE to be 451.

I am using MySQL and Ruby on Rails.

+2  A: 

Is author_website a nullable field?

If 248 rows had null values, this might explain it.

Could you do this instead?

Comment.count(:conditions => ["not(author_website like ?)",'aaaa'])
Adam
Damn, owned again. More concise than my answer and looks like it should work. +1
fd
Null theory is true, however `not( foo like ? )` expression does not change anything (still doesn't include nulls). Just FYI.
Henryk Konsek
+1  A: 

As troelskn is implying with his comment, if a value is NULL then it is neither like nor not like any particular value. Try:

Comment.count(:conditions => ["author_website is null OR author_website not like ?", 'aaaa'])
fd