Why does COUNT() show only one row of table?

2020-05-22 01:15发布

I have the following table pet in the database menagerie:

+--------+-------------+---------+------+------------+------------+
| name   | owner       | species | sex  | birth      | death      |
+--------+-------------+---------+------+------------+------------+
| Tommy  | Salman Khan | Lebre   | NULL | 1999-01-13 | 0000-00-00 |
| Bowser | Diane       | dog     | m    | 1981-08-31 | 1995-07-29 |
+--------+-------------+---------+------+------------+------------+

Now If I run the following query:

select owner, curdate() from pet;  

I get the following output:

+-------------+------------+
| owner       | curdate()  |
+-------------+------------+
| Salman Khan | 2016-09-12 |
| Diane       | 2016-09-12 |
+-------------+------------+

The output show all the values of owner, and the value returned from curdate() in each row.

Now if I run the following query:

select owner, count(*) from pet;  

I get the following output:

+-------------+----------+
| owner       | count(*) |
+-------------+----------+
| Salman Khan |        2 |
+-------------+----------+  

My question is what is the difference between curdate() and count() function which makes MySQL to output the second owner Diane in the first example?

7条回答
▲ chillily
2楼-- · 2020-05-22 01:38

Yes this usually happens with out using the group by clause.

http://www.w3schools.com/sql/sql_groupby.asp You should read in the link all about the group by clause.

All the column should be mentioned either will aggregation or in group by

查看更多
我只想做你的唯一
3楼-- · 2020-05-22 01:39

This looks exactly like the scenario at the bottom of this page: MySQL Documentation: 4.3.4.8 Counting Rows.

If ONLY_FULL_GROUP_BY is not enabled, the query is processed by treating all rows as a single group, but the value selected for each named column is indeterminate. The server is free to select the value from any row:

mysql> SET sql_mode = '';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT owner, COUNT(*) FROM pet;
+--------+----------+
| owner  | COUNT(*) |
+--------+----------+
| Harold |        8 |
+--------+----------+
1 row in set (0.00 sec)

I guess in this case only_full_group_by is not set.

查看更多
Lonely孤独者°
4楼-- · 2020-05-22 01:46

COUNT() is an aggregation function which is usually combined with a GROUP BY clause.

curdate() is a date function which outputs the current date.

Only MySQL (as far as I know of) allows this syntax without using the GROUP BY clause. Since you didn't provide it, COUNT(*) will count the total amount of rows in the table , and the owner column will be selected randomly/optimizer default/by indexes .

This should be your query :

select owner, count(*) 
from pet
group by owner;

Which tells the optimizer to count total rows, for each owner.

When no group by clause mentioned - the aggregation functions are applied on the entire data of the table.

EDIT: A count that will be applied on each row can't be normally done with COUNT() and usually used with an analytic function -> COUNT() OVER(PARTITION...) which unfortunately doesn't exist in MySQL. Your other option is to make a JOIN/CORRELATED QUERY for this additional column.

Another Edit: If you want to total count next to each owner, you can use a sub query:

SELECT owner,
       (SELECT COUNT(*) FROM pet) as cnt
FROM pet
查看更多
做个烂人
5楼-- · 2020-05-22 01:54

Only MySQL will let you make this kind of queries.

You should always specify all the the columns that are not combined with an aggregation function in the GROUP BY clause . If not, the data will be combined into 1 row, with the aggregated columns set correctly, and all the other columns picked randomly or with the indexes.

So you need this :

SELECT owner, count(*) FROM pet GROUP BY owner;

Which will result :

Saknan Khan |  1
Diane       |  1

Is this what you intended to achieve?

Or maybe you tried to do this:

SELECT t.owner,
       COUNT(*) as cnt
FROM pet t
CROSS JOIN pet s
GROUP BY t.owner

Which will result in an additional column with the total count next to each owner.

查看更多
仙女界的扛把子
6楼-- · 2020-05-22 01:54

Most DBMS systems won't allow a aggregate function like count() with additional columns without a group by; for a reason. The DBMS does not know which columns to group :-).

The solution is to group your query by the owner column, like this:

SELECT owner, count(*) FROM pet GROUP BY owner;
查看更多
做个烂人
7楼-- · 2020-05-22 01:59

Count(*) aggregate function it returns only one value and i.e. total number of rows. And curdate() function is just provide the system's current date.

查看更多
登录 后发表回答