2017-08-03 74 views
-1

以下是我的查詢我想顯示在商店的所有項目,即使項目數爲零MySQL的:條件和GROUPBY某些行與零個值

select count(itemn) as count, item from mytable group by item 
This lists 

count  item 
234  1 
    45  2 
    65  3 
    23  4 

但是當我施加一定的條件下,它沒有顯示所有項目

select count(itemn) as count, item from mytable where price <230 group by item 

count  item 
234  1 
    23  4 

但我想要的結果類似

count  item 
234  1 
    0  2 
    0  3 
    23  4 
+0

你可以顯示你在列價格中有什麼值嗎? – nacho

+0

@nacho它的價格與價值像320,450,120,110 –

+0

似乎對於項目編號2(和3),你沒有任何價格<230的行,你應該檢查你的DB – nacho

回答

0

剛剛嘗試做這種方式,則:

select SUM(case 
    when price<230 then 1 
    else 0 
    end) as count, item 
from mytable 
group by item