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

你离开我真会死。 提交于 2020-05-09 19:28:30

问题


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?


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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;



回答5:


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.




回答6:


The last query is invalid for Oracle: ORA-00937: not a single-group function. This means you need a GROUP BY clause. You found a loophole in the MySql implementation. Do not rely on such a query in a production system, in a next version of MySql this might not work.




回答7:


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



来源:https://stackoverflow.com/questions/39444904/why-does-count-show-only-one-row-of-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!