MySQL COUNT() and nulls

后端 未结 5 979
庸人自扰
庸人自扰 2020-12-09 10:05

Am I correct in saying:

COUNT(expr)
WHERE expr IS NOT *  

Will count only non nulls?

Will COUN

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 10:34

    Correct. COUNT(*) is all rows in the table, COUNT(Expression) is where the expression is non-null only.

    If all columns are NULL (which indicates you don't have a primary key, so this shouldn't happen in a normalized database) COUNT(*) still returns all of the rows inserted. Just don't do that.

    You can think of the * symbol as meaning "in the table" and not "in any column".

    This is covered in the MySQL Reference Manual.

提交回复
热议问题