NOT LIKE not working in MySQL with Rails
>> 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.
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'])
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'])
精彩评论