How groupby and count work in sql

1> select browser,count(*) from logtest group by browser;

+-----------+----------+
| browser   | count(*) |
+-----------+----------+
| Firefox 3 |       14 |
| Unknown   |       11 |
+-----------+----------+

2 lines in a set

2> select browser,count(browser) from logtest group by browser;

+-----------+----------------+
| browser   | count(browser) |
+-----------+----------------+
| Firefox 3 |             14 |
| Unknown   |             11 |
+-----------+----------------+

2 lines in a set

3> select browser,count(browser) from logtest;

+-----------+----------------+
| browser   | count(browser) |
+-----------+----------------+
| Firefox 3 |             25 |
+-----------+----------------+

1 row in a set

Why does the query method 1> and 2> produce the same result? Does the difference between counting (*) and count (somefiled) not exist?

Also, if the query 2> and 3> will lead to a different result, why is groupby so magic? How it works?


UPDATE: I am using MySQL5.1. :)

+3
source share
5 answers

Choosing relationally gives you a set of results. If you group your selection by field, the rows of the result set will be grouped by this field, and each row of the result set will be specific to the group of results.

, "" :

Type | Gender | Name

(, MySQL):

 select Type, Gender, Name from Animals where Type <> 'Pig'

, "". Type = 'pig', .

:

select Type, Gender, count(*) from Animals group by Type, Gender

: *

, having MySQL.

count(*) count(browser) , , , not (browser is null).

, browser is null, 1) 2), .

+3

COUNT(*) . , , MySQL MyISAM. COUNT(column_name) , NULL.

, , , . Firefox 3 (14) (11), 25. , , , , , , . .

, RDBMS , GROUP BY . , , COUNT(), MAX(), AVG() ..

+2

count(*) , . count(browser) , . , , .

group by .

0

COUNT(field) . SQL Server , , :

COUNT (*) , NULL .

COUNT ( ALL) .

COUNT ( DISTINCT) .

, groupby . , , . technet.

, 3, SQL. , COUNT AVG , .

0

SQL , . MySQL?

:

1 2 . COUNT (*) . COUNT (FieldName) 1 , FieldName NULL. NULL, .

? 10 NULL , (NULL) | 10, (NULL) | 0.

SQL. "GROUPING BY" , , ? . , , , ( , ).

0
source

Source: https://habr.com/ru/post/1771844/


All Articles