Articulu Directory
MySQL Somma gruppu per usu?basa di dati MySQLSpiegazione dettagliata di u gruppu per contu
MySQL GROUP BY dichjarazione
A dichjarazione GROUP BY raggruppa u set di risultati basatu annantu à una o più colonne.
Nantu à a colonna raggruppata pudemu usà funzioni COUNT, SUM, AVG, etc.
GROUP BY sintassi
SELECT column_name, function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
Esempiu di dimostrazione
L'esempiu in stu capitulu usa a struttura di a tabella è e dati chì seguitanu Prima di usà, pudemu impurtà e seguenti dati in a basa di dati.
SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for `employee_tbl` -- ---------------------------- DROP TABLE IF EXISTS `employee_tbl`; CREATE TABLE `employee_tbl` ( `id` int(11) NOT NULL, `name` char(10) NOT NULL DEFAULT '', `date` datetime NOT NULL, `singin` tinyint(4) NOT NULL DEFAULT '0' COMMENT '登录次数', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of `employee_tbl` -- ---------------------------- BEGIN; INSERT INTO `employee_tbl` VALUES ('1', '小明', '2016-04-22 15:25:33', '1'), ('2', '小王', '2016-04-20 15:25:47', '3'), ('3', '小丽', '2016-04-19 15:26:02', '2'), ('4', '小王', '2016-04-07 15:26:14', '4'), ('5', '小明', '2016-04-11 15:26:40', '4'), ('6', '小明', '2016-04-04 15:26:54', '2'); COMMIT; SET FOREIGN_KEY_CHECKS = 1;
Dopu chì l'importazione hè successu, eseguite a seguente dichjarazione SQL:
mysql> set names utf8; mysql> SELECT * FROM employee_tbl; +----+--------+---------------------+--------+ | id | name | date | singin | +----+--------+---------------------+--------+ | 1 | 小明 | 2016-04-22 15:25:33 | 1 | | 2 | 小王 | 2016-04-20 15:25:47 | 3 | | 3 | 小丽 | 2016-04-19 15:26:02 | 2 | | 4 | 小王 | 2016-04-07 15:26:14 | 4 | | 5 | 小明 | 2016-04-11 15:26:40 | 4 | | 6 | 小明 | 2016-04-04 15:26:54 | 2 | +----+--------+---------------------+--------+ 6 rows in set (0.00 sec)
Dopu avemu usatu a dichjarazione GROUP BY per raggruppà a tabella di dati per nome è cuntà quanti registri ogni persona hà:
mysql> SELECT name, COUNT(*) FROM employee_tbl GROUP BY name; +--------+----------+ | name | COUNT(*) | +--------+----------+ | 小丽 | 1 | | 小明 | 3 | | 小王 | 2 | +--------+----------+ 3 rows in set (0.01 sec)
Aduprà WITH ROLLUP
WITH ROLLUP pò implementà e stesse statistiche (SUM, AVG, COUNT...) nantu à a basa di statistiche raggruppate.
Per esempiu, raggruppemu a tabella di dati sopra per nome, è poi cuntemu u numeru di volte chì ogni persona hà logged in:
mysql> SELECT name, SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP; +--------+--------------+ | name | singin_count | +--------+--------------+ | 小丽 | 2 | | 小明 | 7 | | 小王 | 7 | | NULL | 16 | +--------+--------------+ 4 rows in set (0.00 sec)
U record NULL rapprisenta u numeru di logins per tutti.
Pudemu aduprà coalesce per stabilisce un nome chì pò rimpiazzà NUll, coalesce syntax:
select coalesce(a,b,c);
Descrizione di i paràmetri: se a==null, sceglite b; se b==null, sceglite c; se a!=null, sceglite a; se abc hè null, restituite null (senza significatu).
In l'esempiu seguente, se u nome hè viotu, usemu u tutale invece:
mysql> SELECT coalesce(name, '总数'), SUM(singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP; +--------------------------+--------------+ | coalesce(name, '总数') | singin_count | +--------------------------+--------------+ | 小丽 | 2 | | 小明 | 7 | | 小王 | 7 | | 总数 | 16 | +--------------------------+--------------+ 4 rows in set (0.01 sec)
Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) hà spartutu "Gruppu di somma MySQL per usu? Spiegazione Dettagliata di Gruppu per Conte in MySQL Database" hè d'aiutu per voi.
Benvenuti à sparte u ligame di stu articulu:https://www.chenweiliang.com/cwl-477.html
Benvenuti à u canali Telegram di u blog di Chen Weiliang per avè l'ultime aghjurnamenti!
📚 Questa guida cuntene un valore enormu, 🌟Questa hè una rara opportunità, ùn mancate micca! ⏰⌛💨
Condividi è mi piace se ti piace!
A vostra spartera è Mi piace sò a nostra motivazione cuntinua!