2009-11-03 53 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'])