1> select browser,count(*) from logtest group by browser;
+-----------+----------+
| browser | count(*) |
+-----------+----------+
| Firefox 3 | 14 |
| Unknown | 11 |
+-----------+----------+
2 rows in set
2> select browser,count(browser) from logtest group by browser;
+-----------+----------------+
| browser | count(browser) |
+-----------+----------------+
| Firefox 3 | 14 |
| Unknown | 11 |
+-----------+----------------+
2 rows in set
3> select browser,count(browser) from logtest;
+-----------+----------------+
| browser | count(browser) |
+-----------+----------------+
| Firefox 3 | 25 |
+-----------+----------------+
1 row in set
Why the query manner 1> and 2> result in the same result? Is there nothing difference between the count(*) and count(somefiled)?
Also,whay the query 2> and 3> result in the different result,why the groupby so magic? How does it work?
UPDATE:
I am using MySQL5.1. 🙂
A selection relationally gives you a result set. If you are grouping your selection by a field, the rows of the result set will be grouped by that field and each row of the result set will be specific for the group of the results.
For example you have a table named Animals with the following fields:
If you are running this query (in MySQL, for example):
you’ll get all the animals which are not ‘Pig’. If a row has Type = ‘pig’, it will be included into the results.
This query:
will have this many rows: number of types * number of genders
You can make conditions for your group by’s using the having clause in MySQL.
Read more here
The difference between
count(*)andcount(browser)is that the first will return the number of all records, the second will return the number of all records wherenot (browser is null).Try inserting a row where
browser is nulland then run 1) and 2), this is the best test.