MYSQL counting value availability (non-null)

I have a table like this:

id | module_id | answered 1 4 3 2 4 1 3 4 NULL 4 5 NULL 5 5 1 

I need to calculate the number of rows for each id_ module, and the number of rows that it responds to is not NULL.

so i need a result

 module_id | row_count | answered 4 3 2 5 2 1 

I still have

 SELECT module_id, COUNT(*) as row_count FROM table GROUP BY module_id 

but I don’t know how to make a response column. Any ideas?

+4
source share
2 answers
  COUNT(answered) 

will read non-zero values ​​in this particular column.

This is why COUNT (*) is much faster than COUNT (some column).

+4
source

if you use COUNT (column), it will only count rows with a non-zero value.

The Count function has three options:

  • COUNT (*) counts all rows
  • COUNT (column) only counts non-null numbers
  • COUNT (1) is the same as COUNT (*), since 1 is a non-zero expression

The use of COUNT (*) or COUNT (column) should be based only on the desired output.

0
source

Source: https://habr.com/ru/post/1501330/


All Articles