mysql中的group by的使用方法:【select * from table_name where column_name group by column_name;】。group by语句根据一个或多个列对结果集进行分组。
group by 语句根据一个或多个列对结果集进行分组。
在分组的列上我们可以使用 count, sum, avg,等函数。
(推荐教程:mysql视频教程)
语法格式:
select column_name, function(column_name)from table_namewhere column_name operator valuegroup by column_name;
举例:
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;
导入成功后,执行以下 sql 语句:
接下来我们使用 group by 语句 将数据表按名字进行分组,并统计每个人有多少条记录:
相关推荐:mysql教程
以上就是mysql中的group by如何使用的详细内容。