gotchasqlModerate
Difference between count(*) and count(1) in mysql?
Viewed 0 times
differencemysqlbetweenandcount
Problem
In MySql we can count the total number of records by using count(1) or count(*).
Is there any technical difference between them?
Is there any technical difference between them?
Solution
They are the same. This has often been asked in Stackoverflow and here
all return the count.
In fact, in SQL Server, the expression isn't even evaluated.
Edit In fact, in SQL Server, a
e.g.
but
and at least one exception is
FWR in MySQL
count(*), count(0), count(1), count(-1)all return the count.
In fact, in SQL Server, the expression isn't even evaluated.
Edit In fact, in SQL Server, a
COUNT(ALL ...) CONSTANT expression doesn't appear to be evaluated at all, however, a COUNT(DISTINCT ...) is*.e.g.
select count(ALL 1/0) from xyz; -- Succeedsbut
select count(DISTINCT 1/0) from xyz; -- Divide by Zeroand at least one exception is
NULLselect count(1) from xyz; -- Operand data type void type is invalid for count operator.FWR in MySQL
count(1/0) returns 0 irrespective of the number of rows.Code Snippets
count(*), count(0), count(1), count(-1)select count(ALL 1/0) from xyz; -- Succeedsselect count(DISTINCT 1/0) from xyz; -- Divide by Zeroselect count(1) from xyz; -- Operand data type void type is invalid for count operator.Context
StackExchange Database Administrators Q#22706, answer score: 10
Revisions (0)
No revisions yet.