In SQL, what does Group By mean without Count (*) or Sum (), Max (), avg (), ... and which uses it?

In SQL, if we use Group By without Count (*) or Sum (), etc., the result is as follows:

mysql>  select * from sentGifts;
+--------+------------+--------+------+---------------------+--------+
| sentID | whenSent   | fromID | toID | trytryWhen          | giftID |
+--------+------------+--------+------+---------------------+--------+
|      1 | 2010-04-24 |    123 |  456 | 2010-04-24 01:52:20 |    100 |
|      2 | 2010-04-24 |    123 | 4568 | 2010-04-24 01:56:04 |    100 |
|      3 | 2010-04-24 |    123 | NULL | NULL                |      1 |
|      4 | 2010-04-24 |   NULL |  111 | 2010-04-24 03:10:42 |      2 |
|      5 | 2010-03-03 |     11 |   22 | 2010-03-03 00:00:00 |      6 |
|      6 | 2010-04-24 |     11 |  222 | 2010-04-24 03:54:49 |      6 |
|      7 | 2010-04-24 |      1 |    2 | 2010-04-24 03:58:45 |      6 |
+--------+------------+--------+------+---------------------+--------+
7 rows in set (0.00 sec)


mysql>  select *, count(*) from sentGifts group by whenSent;
+--------+------------+--------+------+---------------------+--------+----------+
| sentID | whenSent   | fromID | toID | trytryWhen          | giftID | count(*) |
+--------+------------+--------+------+---------------------+--------+----------+
|      5 | 2010-03-03 |     11 |   22 | 2010-03-03 00:00:00 |      6 |        1 |
|      1 | 2010-04-24 |    123 |  456 | 2010-04-24 01:52:20 |    100 |        6 |
+--------+------------+--------+------+---------------------+--------+----------+
2 rows in set (0.00 sec)


mysql>  select * from sentGifts group by whenSent;
+--------+------------+--------+------+---------------------+--------+
| sentID | whenSent   | fromID | toID | trytryWhen          | giftID |
+--------+------------+--------+------+---------------------+--------+
|      5 | 2010-03-03 |     11 |   22 | 2010-03-03 00:00:00 |      6 |
|      1 | 2010-04-24 |    123 |  456 | 2010-04-24 01:52:20 |    100 |
+--------+------------+--------+------+---------------------+--------+
2 rows in set (0.00 sec)

Only 1 row is returned to the "group". What does this mean when there is no "Count (*)", etc. When using "Group By" and what does it use? thank.

+3
source share
2 answers

By default, MySQL will return the value of the first row it encounters when executing a query. It kind of uses the default aggregate arbitrary.

This is useful if you have a long list of columns in which you know that most of them are repeated, for example:

Login    LongName             City        PhoneNr      Time
Dude     Mr. Dude the 2nd     Jerk Town   12345678     13:01
Dude     Mr. Dude the 2nd     Jerk Town   12345678     13:05
Dude     Mr. Dude the 2nd     Jerk Town   12345678     13:12

group by login:

select LongName, City, PhoneNr, max(Time) from Logins group by login

, Long Name Login, , . , , group by login, LongName, City, PhoneNr. MySQL, .

+3

group by select select distinct, -.

BTW, select * from sentGifts group by whenSent MySQL: select group by ( PostgreSQL , , MSSQL ). "MySQL", " SQL", , , , , SQL.

0

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


All Articles