2017-03-22 24 views
1

我正在嘗試爲我的板製作圖表。 但我不知道如何才能計算article_id的數量,只有當它不爲空。MySQL - 用於除零的數除外

在下面,如果article_id爲null,則不應該被計數。 我該如何解決這個問題?

mysql> select article_id, count(*) as count from board group by article_id order by count desc limit 3; 
+------------+-------+ 
| article_id | count | 
+------------+-------+ 
|  NULL |  7 | 
|   12 |  3 | 
|   3 |  2 | 
+------------+-------+ 
3 rows in set (0.00 sec) 

回答

3

篩選出空了WHERE article_id IS NOT NULL

+1

非常感謝! – KimNR

3
select article_id, count(*) as count 
from board 
where 
article_id is not null 
group by article_id 
order by count desc limit 3 
1

你應該嘗試 的mysql>選擇的article_id,COUNT(*)作爲通過的article_id從板組數,其中article_is是不計數遞減限空順序3;