赞
踩
- SELECT queryitem, function(queryitem)
- FROM table
- WHERE ...
- GROUP BY queryitem;
语法解析:
- -- 创建表
- CREATE TABLE `test` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `name` varchar(20) COLLATE utf8mb4_bin DEFAULT '姓名',
- `sign_in_date` datetime DEFAULT NULL COMMENT '签到时间',
- `sign_in_count` int(11) DEFAULT 1 COMMENT '签到次数',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
-
-
- -- 插入数据
- INSERT INTO `test`(name, sign_in_date, sign_in_count)
- VALUES
- ('小明', '2021-11-22 00:00:00', 1),
- ('小王', '2021-11-22 00:00:00', 2),
- ('小丽', '2021-11-22 00:00:00', 3),
- ('小明', '2021-11-23 00:00:00', 4),
- ('小王', '2021-11-23 00:00:00', 5),
- ('小丽', '2021-11-23 00:00:00', 6),
- ('小明', '2021-11-24 00:00:00', 7),
- ('小王', '2021-11-25 00:00:00', 8),
- ('小明', '2021-11-26 00:00:00', 9);
效果图:
- -- 打卡天数
- SELECT NAME, count(1) FROM test GROUP BY NAME;
效果:
- -- 打卡次数
- SELECT NAME, SUM( sign_in_count ) AS countSum FROM test GROUP BY NAME WITH ROLLUP;
效果(其中 NAME IS NULL 表示所有人的登录次数《总登陆次数》):
- -- 打卡次数
- SELECT COALESCE( NAME, '总数' ), SUM( sign_in_count ) AS countSum FROM test GROUP BY NAME WITH ROLLUP;
效果:
注:以上内容仅提供参考和交流,请勿用于商业用途,如有侵权联系本人删除!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。