Doubts about the count sql command

4

When I write the command select count(carga > 40) from cursos; what is the count logic that the count command is running?

This command worked in mysql however it returned me a result that I could not identify the source. I could not understand what the count identified on the basis.

    
asked by anonymous 16.09.2018 / 03:25

3 answers

4

count(expressao) does not fit what you're trying to do.

It returns no "true" or "false" account, but "how many instances of expressão are not null."

The count(*) is an exception, it returns all rows, regardless of nullity.

Manual:

  

link


Two solutions for you:

SELECT COUNT(*) FROM tabela WHERE carga > 40;

or

SELECT SUM(IF(carga > 40, 1, 0 ));


This second, with if , can be better understood in this post:

  

link

    
16.09.2018 / 03:40
1

I would do it as follows

select count(*) from nometabela where carga > 40;

In this example it will count the records that were selected The selected records would all be the carga value greater than 40

 select count(carga) from nometabela where carga > 40;

If you put carga instead of * you may have problems if loading is null

    
16.09.2018 / 03:39
1

Count () returns the line numbers. For example 5 records with load > 40 , then it will return 5.

To sum the value of the 5 registers for example use sum ().

    
16.09.2018 / 03:32