Tuhinga Tuhinga
MySQL Huihia te roopu ma te whakamahi?pātengi raraunga MySQLHe whakamaramatanga mo te roopu ma te tatau
MySQL GROUP BY tauākī
Ka whakarōpūhia e te tauākī GROUP BY te huinga hua i runga i te kotahi, neke atu rānei o ngā tīwae.
I runga i te rarangi whakarōpū ka taea e tatou te whakamahi i nga mahi COUNT, SUM, AVG, etc.
Rōpū MA te wetereo
SELECT column_name, function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
Whakaaturanga tauira
Ko te tauira o tenei upoko e whakamahi ana i te hanganga ripanga me nga raraunga e whai ake nei.
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;
Kia angitu te kawemai, mahia te korero SQL e whai ake nei:
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)
I muri mai ka whakamahia e matou te korero RUPAPA MA te whakarōpū i te ripanga raraunga ma te ingoa me te tatau e hia nga rekoata kei ia tangata:
mysql> SELECT name, COUNT(*) FROM employee_tbl GROUP BY name; +--------+----------+ | name | COUNT(*) | +--------+----------+ | 小丽 | 1 | | 小明 | 3 | | 小王 | 2 | +--------+----------+ 3 rows in set (0.01 sec)
Whakamahia me te ROLLUP
Ka taea e WITH ROLLUP te whakatinana i nga tatauranga rite (SUM, AVG, COUNT...) i runga i nga tatauranga kua whakarōpūhia.
Hei tauira, ka whakarōpūhia te ripanga raraunga i runga ake ma te ingoa, ka tatauhia te maha o nga wa i uru mai ai ia tangata:
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)
Ko te rekoata NULL e tohu ana i te maha o nga takiuru mo te katoa.
Ka taea e tatou te whakamahi coalesce ki te tautuhi ingoa hei whakakapi i te NUll, coalesce syntax:
select coalesce(a,b,c);
Whakaahuatanga tawhā: ki te a==null, whiriwhiria b; ki te b==null, kowhiria c; ki te a!=null, kowhiria a; ki te kore abc, whakahokia kore (kore tikanga).
I te tauira e whai ake nei mena kei te putua te ingoa ka whakamahia e matou te tapeke:
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/ ) shared "MySQL sum group by use? Ko te Whakamaramatanga Taipitopito mo te Roopu ma te Tatau i roto i te Raraunga Raraunga MySQL" he awhina ki a koe.
Nau mai ki te tohatoha i te hono o tenei tuhinga:https://www.chenweiliang.com/cwl-477.html
Nau mai ki te hongere Telegram o te blog a Chen Weiliang ki te tiki i nga korero hou!
📚 He uara nui tenei aratohu, 🌟He waahi onge tenei, kaua e ngaro! ⏰⌛💨
Tohaina me te pai ki te pai koe!
Ko to tiritiri me o hiahia ko to maatau hihiri tonu!