2009-11-03 51 views
0
>> 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 
>> 

我期待的不是像计数为451不喜欢的MySQL不工作和Rails

我使用MySQL和Ruby on Rails。

+1

'author_website'可以为null吗? – troelskn 2009-11-03 17:01:56

回答

2

author_website是否为空字段?

如果248行有空值,这可以解释它。

你可以这样做吗?

Comment.count(:conditions => ["not(author_website like ?)",'aaaa']) 
+0

该死的,再次拥有。比我的答案更简洁,看起来应该起作用。 +1 – 2009-11-03 17:13:44

+0

空理论是真实的,然而'不(像?)'表达不会改变任何东西(仍然不包括空值)。只是FYI。 – 2009-11-03 18:13:08

1

由于troelskn暗示与他的评论,如果一个值是NULL,那么它既不喜欢也不不喜欢任何特定的值。试试:

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