2013-09-05 31 views
5

Im cercando di ottenere questo risultato:Mysql - Il conteggio NULL restituisce sempre 0?

NULL 350 
google 98 
yahoo 5 
bing 4 

Con questa interrogazione:

SELECT engine, COUNT(engine) AS count 
FROM visits 
GROUP BY engine 
ORDER BY count DESC 

ma restituisce:

google 98 
yahoo 5 
bing 4 
NULL 0 

come posso risolvere questo problema?

+3

'COUNT (*)' o 'COUNT (1)' o 'COUNT (aNotNullableColumn)' funzionerà. –

risposta

3

Uso COUNT(1) invece:

SELECT engine, COUNT(1) AS count 
FROM visits 
GROUP BY engine 
ORDER BY count DESC; 
+0

WOW, questa è una risposta rapida e corretta! Grazie mille =) BTW, perché ha funzionato? – Sultanen

+1

@Sultanen - Prego in qualsiasi momento :) –

+0

@ConteSananen (colname) conta ogni riga che contiene un valore non nullo (!) - in colname. count (1) può essere pensato per aggiungere una colonna aggiuntiva con valore 1 in ogni riga del risultato della query. –

Problemi correlati